When I use below code query database, I cannot get any result, but I can get results via sql command on console.
DBHelper dbHelper = new DBHelper(SampleActivity.this, "contents.db", null, 1);
SQLiteDatabase db = dbHelper.getWritableDatabase();
//Cursor cursor = db.query("ContentsTable", new String[] { "title" }, "id<10", null, null, null, null);
Cursor cursor = db.rawQuery("select title from ContentsTable", null);
db.close();
cursor.close();
I debug the program and found cursor’s mCount=-1, and mStackTrace=DatabaseObjectNotClosedException after running db.rawquery(*), just like the photo showed:http://flic.kr/p/bw9P2o
Below is class DBHelper:
public class DBHelper extends SQLiteOpenHelper{
public DBHelper(Context context, String name, CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
String createTable = "create table ContentsTable(id int, title varchar(100))";
db.execSQL(createTable);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
System.out.println("Update Database");
}
}
Forgive the simple question but have you added rows yet?
EDIT:
You have to call
cursor.moveToFirst()before accessing the data returned.