0

I was following this tutroial about using your own SQLite database. I did these steps as described in the tutorial:

  • Preparing my database
  • Copying the same EXACT DataBaseHelper class to my project package. You can see the class code there
  • Then, I just added one method to the DataBaseHelper class which is fetchData. It is simply fetching a whole table with the given name:

    public Cursor fetchData(String table) {
    String[] selectionArgs = new String[] {"*"};
    return myDataBase.query(table, null, null, selectionArgs, null, null, null);
    }
    
  • After that, in one of my activity classes I did this:

    DataBaseHelper myDbHelper;
    myDbHelper = new DataBaseHelper(this);
    
    try { 
    
        myDbHelper.createDataBase();
    
    } catch (IOException ioe) {
    
        throw new Error("Unable to create database");
    
    }
    try {
    
        myDbHelper.openDataBase();
    
    } catch (SQLException sqle) {
    
        throw sqle;
    
    }
    
    TextView txt = (TextView) findViewById(R.id.txt);
    try {
    //I will use my method to fetch a table named: myTable      
        Cursor c = myDbHelper.fetchData("myTable"); 
        if((Object)c.getCount() != null)
            txt.setText(c.getCount());
        else
            txt.setText("null");
    } catch(Exception e) {
        txt.setText("error");
    
    } 
    

However, I keep getting 'error' in the TextView. Is there a problem in my way?

3 Answers 3

1

My problem is nothing related to SQLite. It was silly mistake :-\

The error is in the second line here:

if((Object)c.getCount() != null)
    txt.setText(c.getCount());

It must be like this:

    txt.setText(""+c.getCount());

the setText() method accepts a ChaSequence and the getCount() method returns Integer which are not in compatible type. you can work around that tha easy way, by adding empty string :)

Thanks Guys.

Sign up to request clarification or add additional context in comments.

Comments

0
public Cursor fetchData(String table) {
     return myDataBase.query(table, null, null, null, null, null, null);
}

Seems you got wrong idea about the selectionArgs parameter. In documentation, it says:

You may include ?s in selection, which will be replaced by the values from selectionArgs, in order that they appear in the selection. The values will be bound as Strings.

4 Comments

That was my first trial but it didn't work. I tried this instead and got the same. Thanks for your try.
As your app is going to the catch part, check what exception you are getting.
I figured it out :) See my answer
Unfortunately, I can't answer before 7 hours :(
0

Your trying to cast an int as an Object, then comparing the cast value to null. I would say that you code is likely to break right there.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.