当我使用下面的代码查询数据库时,我无法得到任何结果,但我可以通过控制台上的 sql 命令获得结果。
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();
我调试程序,运行db.rawquery( * )后发现游标的mCount=-1和mStackTrace=DatabaseObjectNotClosedException ,就像照片所示: http: //flic.kr/p/bw9P2o
下面是 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");
}
}