Problems with SQLiteOpenHelper

I have worked with Android and SQLite Database to develop an application. But I have several 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 am getting the data that is stored in Category

my database table . But I am not getting any data in cursorTransaction

, why is this happening?

Also, when instantiating my object SQLiteHelperMoney2

, if I change the object instance SQLiteHelperMoney2

to this:

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

then I am getting data from a table Transaction

in my cursorTransaction

.

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

In addition, the documentation says that in the SQLiteOpenHelper constructor, in the "NAME" field, you can indicate null

if the database is already in memory. I assumed the database was created in the application itself, so it will only be in memory, so I am specifying the second parameter here as null:

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

But it works here, but not otherwise.

Also, what is the default database name? Because I didn't specify it in my onCreate

SQLiteOpenHelper function . 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 does SQLiteOpenHelper onCreate () / onUpgrade () work?

And I am a little confused about the on-disk database and in memory. In my method, onCreate

I add rows to the table Category

because I thought the onCreate method was only called once during the life of the application, i.e. When it runs for the first time to create databases. This is why in my method onCreate

I am adding data to the table Category

, because I wanted it to have the original data.

And I believed that when we call replace calls getReadableDatabase()

and getWritableDatabase()

, it returns us a copy of the previously created database that we are working with, or perhaps a handle to the original database, and saves those changes when done. But after reading your answer, it seems like the methods onCreate

are being run every time. If they run every time where is the data stored since we will lose the database when we close it?

+3


source to share


1 answer


Now the problem is that in my cursorCategory I am getting the data that is stored in the Category table of my database. But I am not getting any data in cursorTransaction, why is this happening?

The query does not return data from the newly created in-memory database.

Also, when instantiating my SQLiteHelperMoney2 object, if I change the instance of the SQLiteHelperMoney2 object to this:

...

But why is this happening?

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

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

In addition, the documentation says that in the SQLiteOpenHelper constructor, you can specify null in the "NAME" field if the database is already in memory. I assumed the database was created in the application itself, so it will only be in memory



Passing zero creates a new in-memory database. This means a new, empty database that is not saved 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 otherwise.

Perhaps because the other helper fills the database in his own onCreate()

and the other does not.

Also, what is the default database name?

Nothing. In-memory databases have no name. Named databases are saved to disk with the name you specified.

+3


source







All Articles