Android:如何使用非字符串选择args查询SQLiteDatabase? [英] Android: how to query SQLiteDatabase with non-string selection args?
问题描述
是否可以使用非String
类型的选择参数查询SQLiteDatabase
的简单方法?
Is there a straightforward way to query an SQLiteDatabase
with selection args that are not String
types?
特别是:如果arg是byte[]
类型?
Particularly: if the arg is a byte[]
type?
我能找到的最接近的东西是SQLiteDatabase.compileStatement()
,它返回一个SQLiteStatement
您可以调用bindBlob
等.不幸的是,SQLiteStatement
不适用于查询,因为它不返回游标.
The closest thing I can find is SQLiteDatabase.compileStatement()
, which returns an SQLiteStatement
you can call bindBlob
etc. on. Unfortunately SQLiteStatement
isn't good for querying because it doesn't return a cursor.
SQLiteCursor
看起来很有前途,但我不知道如何使用它.
SQLiteCursor
looks promising but I can't figure out how to use it.
推荐答案
这是Android数据库API中的一个设计错误.
This is a design bug in the Android database API.
query
和rawQuery
仅接受字符串参数.
execSQL
接受任何Object
参数,但不返回结果.
SQLiteStatement
接受任何类型的参数,但仅允许返回单个值的查询.
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.
您可以做的是忽略参数,并手动将blob格式化为 blob文字:>
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);
这篇关于Android:如何使用非字符串选择args查询SQLiteDatabase?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!