Reputation: 614
I am working with SQLite Database in android studio. My onCreate code:
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_MOODS_TABLE = "CREATE TABLE " + TABLE_NAME_MOODS + "("
+ COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMN_MOODS + " TEXT NOT NULL)";
db.execSQL(CREATE_MOODS_TABLE);
}
and my Queries:
@Override
public Uri insert(@NonNull Uri uri, ContentValues values) {
final SQLiteDatabase db = mMindsDbHelper.getWritableDatabase();
final SQLiteDatabase db2 = mMindsDbHelper.getReadableDatabase();
long id = db.insert(TABLE_NAME_MOODS, null, values);
String query = "SELECT * FROM "+ TABLE_NAME_MOODS;
Cursor returnCursor;
returnCursor = db2.rawQuery(query, null);
return null;
}
For debugging purpose I have added select query in this place. when debugger hits these points, return id of db.insert returns some value(i.e. id of new inserted values). But select query doesn't return anything useful(it returns mCount as -1). I have tries following query also but result is same.
returnCursor = db.query(TABLE_NAME_MOODS,
projection,
selection,
selectionArgs,
null,
null,
sortOrder);
Where is the problem.
Upvotes: 0
Views: 50
Reputation: 152787
But select query doesn't return anything useful(it returns mCount as -1)
rawQuery()
compiles a query but does not execute it. Hence the count is -1 for a query that has not been executed yet. The same applies to query()
since it's essentially just a wrapper for rawQuery()
. A query that was executed and matched no records would have its count set to 0.
To actually execute at least one step of a query compiled with rawQuery()
, you need to call one of the moveTo...()
methods on the returned Cursor
. For example, moveToFirst()
.
Upvotes: 1