Android: how to query SQLiteDatabase with non-string selection args?

前端 未结 2 354
无人及你
无人及你 2021-01-02 23:17

Is there a straightforward way to query an SQLiteDatabase with selection args that are not String types?

Particularly: if the arg is a

相关标签:
2条回答
  • 2021-01-02 23:49

    also you may try

    Cursor cs = db.rawQueryWithFactory(new CursorFactory() {
                @SuppressLint("NewApi")
                @SuppressWarnings("deprecation")
                @Override
                public Cursor newCursor(SQLiteDatabase db, SQLiteCursorDriver masterQuery,
                        String editTable, SQLiteQuery query) {
    
                    // bind it here!...
                    query.bindBlob(1, mac);
    
    
    
                    if (Build.VERSION.SDK_INT < 11) {
                        return new SQLiteCursor(db, masterQuery, editTable, query);
                    } else {
                        return new SQLiteCursor(masterQuery, editTable, query);
                    }
                }
            }, query, null, DB_TABLE);
    
    0 讨论(0)
  • 2021-01-02 23:58

    This is a design bug in the Android database API.

    query and rawQuery accept only string parameters. execSQL accepts any Object parameters, but does not return results. SQLiteStatement accepts parameters of any type, but allows only queries that return a single value.

    What you can do is to ignore parameters, and format the blobs as blob literals by hand:

    cursor = db.rawQuery("SELECT a FROM b WHERE c = x'112233'", null);
    
    0 讨论(0)
提交回复
热议问题