【发布时间】:2012-04-06 15:23:13
【问题描述】:
当我使用下面的代码查询数据库时,我无法获得任何结果,但我可以通过控制台上的 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(*)后发现cursor的mCount=-1,mStackTrace=DatabaseObjectNotClosedException,如图所示:http://flic.kr/p/bw9P2o
下面是 DBHelper 类: 公共类 DBHelper 扩展 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");
}
}
【问题讨论】: