SQLiteOpenHelper problems

192 views Asked by At

I have been working with Android and SQLite Database to develop an application. But I am having a few problems. This is my code:

        SQLiteHelperMoney SQLiteHelperMoney1 = new SQLiteHelperMoney(this, null, null, 1);
        SQLiteDatabase database = SQLiteHelperMoney1.getReadableDatabase();
        Cursor cursorCategory = database.query(false, "CATEGORY", null, null, null, null, null, null, null);

        SQLiteHelperMoney SQLiteHelperMoney2 = new SQLiteHelperMoney(this, null, null, 1);
        SQLiteDatabase database2 = SQLiteHelperMoney2.getReadableDatabase();
        Cursor cursorTransaction = database2.query(false, "TRANSACTIONS", null, null, null, null, null, null, "10");

Now the problem is, that in my cursor cursorCategory I get the data that is stored in the Category table of my database. But I do not get any data in the cursorTransaction, why does this happen??

Furthermore, while instantiating my SQLiteHelperMoney2 object, if I change the instantiation of my SQLiteHelperMoney2 object to this :

SQLiteHelperMoney SQLiteHelperMoney2 = new SQLiteHelperMoney(this, "TRANSACTIONS", null, 1);

then I get the data from the Transaction table, in my cursorTransaction.

But why does this happen?? According to the documentation, in the constructor of the SQLiteOpenHelper, the second parameter, is the name of the database, not the name of the table. Then why does giving the name of the TABLE, in the field NAME OF DATABASE, gives the correct answer?

Furthermore, the documentation says that in the constructor for SQLiteOpenHelper, in the field "NAME" you can specify null if the database is already in the memory. I was assuming that the database had been created in the app itself, so it will be in memory only, that is why I specify the second parameter as null here :

SQLiteHelperMoney SQLiteHelperMoney1 = new SQLiteHelperMoney(this, null, null, 1);

But it works here but not in the other case.

Further, what is the name of database created by default? Because I haven't specified it in my onCreate function of the SQLiteOpenHelper. And can I change the name of my database??

EDIT:

onCreate method:

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE CATEGORY (_ID INTEGER PRIMARY KEY, NAME TEXT)");
    db.execSQL("CREATE TABLE TRANSACTIONS (_ID INTEGER PRIMARY KEY, AMOUNT INTEGER, CATEGORY TEXT, NOTE TEXT, TIME TEXT, EXPENSE INTEGER)");
    db.execSQL("CREATE TABLE BUDGET (_ID INTEGER PRIMARY KEY, AMOUNT INTEGER, TIME TEXT)");

    ContentValues cv = new ContentValues();
    cv.put("NAME", "FOOD");
    db.insert("CATEGORY", null, cv);
    cv.put("NAME", "TRAVEL");
    db.insert("CATEGORY", null, cv);
    cv.put("NAME", "INCOME");/*INCOME*/
    db.insert("CATEGORY", null, cv);
    cv.put("NAME", "TRANSPORTATION");
    db.insert("CATEGORY", null, cv);
    cv.put("NAME", "MOVIE");
    db.insert("CATEGORY", null, cv);
}

@laalto: I read your answer here : When is SQLiteOpenHelper onCreate() / onUpgrade() run?

And I'm a bit confused about the on-disk and in-memory database. In my onCreate method, I add rows in my Category table, because I believed that the onCreate method is only called once in the lifetime of the application, i.e., when it is run the first time, for creating the databases. That is why in my onCreate method I add data into my CATEGORY table, because I wanted it to have some initial data.

And I believed when we make substituent calls to getReadableDatabase() and getWritableDatabase() it returns us a copy of the earlier created database for us to work with, or probably a handle to the original database and on completion saves those changes. But, after reading your answer it seems that the onCreate methods are run every time. If they are run everytime where does the data get stored, since we will loose the database when we close it??

1

There are 1 answers

4
laalto On BEST ANSWER

Now the problem is, that in my cursor cursorCategory I get the data that is stored in the Category table of my database. But I do not get any data in the cursorTransaction, why does this happen??

The query returns no data from in the in-memory database you just created.

Furthermore, while instantiating my SQLiteHelperMoney2 object, if I change the instantiation of my SQLiteHelperMoney2 object to this :

...

But why does this happen??

The query returns some data from the on-disk database that has been previously created.

For some coincidence the name of the database file is the same as the table. Uninstall and reinstall your app, or clear its data in the app manager to get rid of possible old database files possibly still around.

Furthermore, the documentation says that in the constructor for SQLiteOpenHelper, in the field "NAME" you can specify null if the database is already in the memory. I was assuming that the database had been created in the app itself, so it will be in memory only

Passing a null creates a new in-memory database. It means a new, empty database that is not persisted to disk. When the database is closed, the data is gone. If you create another in-memory database, it won't see the data in other in-memory databases.

But it works here but not in the other case.

Possibly because the other helper populates the database in its onCreate() and the other doesn't.

Further, what is the name of database created by default?

Nothing. In-memory databases have no name. Named databases get stored on disk with the name you gave.