加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

从SQLite数据库Android SDK获取随机行

发布时间:2020-12-12 18:55:41 所属栏目:百科 来源:网络整理
导读:我想从我创建的数据库中获取一个随机行,但我无法弄清楚如何做到这一点. 我在StackOverflow上找到了几个问题和答案,我说应该使用它: Cursor cursor = this.db.query("mainTable Order BY RANDOM() LIMIT 1",new String[] { "*" },null,null); 但是,我认为我
我想从我创建的数据库中获取一个随机行,但我无法弄清楚如何做到这一点.

我在StackOverflow上找到了几个问题和答案,我说应该使用它:

Cursor cursor = this.db.query("mainTable Order BY RANDOM() LIMIT 1",new String[] { "*" },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,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,null);
        if (c != null) {
            c.moveToFirst();
        }
        return c;
    }

    public Cursor getRow(long rowId) {
        String where = KEY_ROWID + "=" + rowId;
        Cursor c =  db.query(true,null);
        if (c != null) {
            c.moveToFirst();
        }
        return c;
    }

    public Cursor getRowRandom(String rowId) {
        Cursor c =  db.query(true,"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,null) != 0;
    }

    private static class DatabaseHelper extends SQLiteOpenHelper
    {
        DatabaseHelper(Context context) {
            super(context,DATABASE_NAME,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);

返回未定义的错误.

那么我需要更改数据库适配器代码才能正确使用此代码?

解决方法

尝试删除它并确保您的db是SQLiteDatabase.您还可以通过以下方式获取随机行:

db.rawQuery("SELECT * FROM mainTable ORDER BY RANDOM() LIMIT 1",null);

顺便说一句,您已经在代码中使用了正确查询的getRowRandom方法.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读