4

DO you know any workaround how to precompile SELECT statement returning Cursor and to use it multiple times (I'm 99% sure that it's possible with sqlite as java desktop driver allows precompilation with SELECT returning cursor)? Unfortunately as source I can't use QueryBuilder but pure SQL String type SELECT statement (with ? for parameters). Precompilation would be great because of efficiency.

Plus what means in practice that SQLiteStatement is not thread safe? How could I replace it to be threads safe and efficient (precompilation)?

4

3 回答 3

1

要预编译语句,请使用compileStatement 函数

SQLiteStatement stmt = db.compileStatement("SELECT X FROM Y WHERE Z = ?");
...
stmt.bindString(1, "foo");
String x = stmt.simpleQueryForString();

不幸的是,不可能从这样的预编译语句中返回游标。如果要获取多行或多列,则必须使用queryor rawQuery

请注意,SQLite 相当快;您不应该费心优化语句编译,除非您实际测量它是您应用程序中的瓶颈。

于 2013-10-10T08:29:33.053 回答
0

我认为这rawQuery()可能会在内部预编译该语句。

SQLiteStatement与支持更多数据类型相比,参数必须转换为字符串。

于 2014-10-30T11:19:43.020 回答
0

我不清楚使用可替换参数(? 占位符)预编译 SELECT 是否比使用 SQLiteDatabase.query() 发出查询更有效。是什么导致您相信您需要编译该声明?

于 2013-10-10T00:56:56.103 回答