我想從我創建的數據庫中獲取一個隨機行,但我無法弄清楚如何去做。從SQLite數據庫Android獲得隨機行Android SDK
我發現了幾個問題和答案在這裏StackOverflow上哪說我應該使用這樣的:
Cursor cursor = this.db.query("mainTable Order BY RANDOM() LIMIT 1",
new String[] { "*" }, null, null, null, null, null);
不過,我覺得我有某種其他的數據庫,因爲它返回一個錯誤,不知道的方法「查詢」 。
這是數據庫的代碼,我使用:
package com.example.database;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBAdapter{
private static final String TAG = "DBAdapter";
public static final String KEY_ROWID = "_id";
public static final int COL_ROWID = 0;
public static final String KEY_PIC = "picture";
public static final String KEY_ANS = "answer";
public static final int COL_PIC = 1;
public static final int COL_ANS = 2;
public static final String[] ALL_KEYS = new String[] {KEY_ROWID, KEY_PIC, KEY_ANS};
public static final String DATABASE_NAME = "MyDb";
public static final String DATABASE_TABLE = "mainTable";
public static final int DATABASE_VERSION = 2;
private static final String DATABASE_CREATE_SQL =
"create table " + DATABASE_TABLE
+ " (" + KEY_ROWID + " integer primary key autoincrement, "
+ KEY_PIC + " integer not null, "
+ KEY_ANS + " text not null"
+ ");";
private final Context context;
private DatabaseHelper myDBHelper;
private SQLiteDatabase db;
public GevaarherkenningDBHelper(Context ctx) {
this.context = ctx;
myDBHelper = new DatabaseHelper(context);
}
public GevaarherkenningDBHelper open() {
db = myDBHelper.getWritableDatabase();
return this;
}
public void close() {
myDBHelper.close();
}
public long insertRow(int pic, String answer) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_PIC, pic);
initialValues.put(KEY_ANS, answer);
return db.insert(DATABASE_TABLE, null, initialValues);
}
public boolean deleteRow(long rowId) {
String where = KEY_ROWID + "=" + rowId;
return db.delete(DATABASE_TABLE, where, null) != 0;
}
public void deleteAll() {
Cursor c = getAllRows();
long rowId = c.getColumnIndexOrThrow(KEY_ROWID);
if (c.moveToFirst()) {
do {
deleteRow(c.getLong((int) rowId));
} while (c.moveToNext());
}
c.close();
}
public Cursor getAllRows() {
String where = null;
Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS,
where, null, null, null, null, null);
if (c != null) {
c.moveToFirst();
}
return c;
}
public Cursor getRow(long rowId) {
String where = KEY_ROWID + "=" + rowId;
Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS,
where, null, null, null, null, null);
if (c != null) {
c.moveToFirst();
}
return c;
}
public Cursor getRowRandom(String rowId) {
Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS,
null, null, null, null, "RANDOM()", "1");
if (c != null) {
c.moveToFirst();
}
return c;
}
public boolean updateRow(long rowId, int pic, String answer) {
String where = KEY_ROWID + "=" + rowId;
ContentValues newValues = new ContentValues();
newValues.put(KEY_PIC, pic);
newValues.put(KEY_ANS, answer);
return db.update(DATABASE_TABLE, newValues, where, null) != 0;
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase _db) {
_db.execSQL(DATABASE_CREATE_SQL);
}
@Override
public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading application's database from version " + oldVersion
+ " to " + newVersion + ", which will destroy all old data!");
_db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(_db);
}
}
}
有沒有一種方法,我可以實現「query'法在此數據庫類,如果是這樣,如何?還是有人建議使用其他數據庫代碼?
編輯:
我可能還沒有問正確的問題在這裏...我的問題是,使用任何在該點的答案給出的方法,如
db.rawQuery("SELECT * FROM mainTable ORDER BY RANDOM() LIMIT 1", null);
返回錯誤,它是未定義的。
那麼我需要在數據庫適配器代碼中更改以正確使用此代碼?
是的,我補充說,我只是試圖忘記從代碼中刪除它。然而,它不起作用... – Thijs93H
你確定你創建了一個'GevaarherkenningDBHelper'的新實例,名爲'open()',之後執行你的查詢嗎? – nikis
工作!愚蠢的我忘了關閉活動中的光標... -_- – Thijs93H