0
以下のコードクエリデータベースを使用すると結果は得られませんが、コンソールでSQLコマンドを使用して結果を得ることができます。query/rawqueryを使用して結果を取得できません#SQLite#
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クラス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");
}
}
はい、コンソール上でsqlite3コマンド(ContentsTableからタイトルを選択)を使用して結果を得ることができるので、 – Jake
[OK]をクリックし、最初にカーソルを閉じます。または、あなたのアクティビティでstartManagingCursor()を使用してみることをお勧めします。 – Sam
すべての情報は実行後に取得されました:Cursor cursor = db.rawQuery( "Select title from ContentsTable"、null); db.close()またはcursor.close();を実行しないでください。 – Jake