Android: SQLite Query not binding integer parameters?

跟風遠走 提交于 2019-12-21 19:49:31

问题


I have problems when making queries with parameters to DB on Android platform (2.2). I have created table like this:

db.execSQL("CREATE VIRTUAL TABLE " + Msg._TABLE_NAME + " USING FTS3 ("
            + Msg._ID + " INTEGER, "
            (...)
            + Msg.READ + " SHORT DEFAULT 0,"
            + Msg.URGENT + " SHORT DEFAULT 0"
            + ");");

Then I am trying to query this using parametrized query:

String[] columns = new String[] {Msg.ROWID, Msg.TITLE, Msg.READ, Msg.URGENT};
(...)
getContentResolver().query(Msg.CONTENT_URI, columns, 
    Msg.URGENT + "=? AND " + Msg.READ + "=?" + , whereArgs, null);

where whereArgs varies for each query:

String[] urgentUnread = new String[]{"1", "0"};
String[] regularUnread = new String[]{"0", "0"};

but no matter what it returns 0 results/rows even though data exist. The content provider does not change the params and the query returns nothing using QueryBuilder as well as when calling query "directly":

Cursor c = db.query(tables, columns, where, whereArgs, groupBy, having, orderBy, limit);

The query works if I do just String concat:

getContentResolver().query(Msg.CONTENT_URI, columns, 
    Msg.READ + "=0 AND " + Msg.URGENT + "=1", null, null);

but that seems to kill the purpose of param queries and is nasty to cache. Dalvik complains (after making lot of queries) that there is no space in cache for query and, ironically, tells me to use parametrized queries with '?'. I would love to, trust me :)

I know JavaDoc states that parameters are bound as StringS but I just simply can't believe that... because that would be major ...ahem, ... WTF

Where did I go wrong here?

Thanks in advance.


回答1:


This is OP, I was researching and experimenting further and came to conclusion that there is FTS3 to blame. Since I need the data to be searchable by fulltext I was creating VIRTUAL TABLE USING FTS3 and then the parameters binding failed.

As I do not want to query shadow table (Msg_content) directly, my solution is to split data into 2 related tables:

db.execSQL("CREATE TABLE " + Msg._TABLE_NAME + " (" +
    Msg._ID + PRIMARY_KEY_AUTOINC + 
    Msg.PRIORITY + " TEXT," +
    Msg.RECEIVED + " INTEGER," +
    Msg.MOBILE_STATUS + " INTEGER DEFAULT 0," +
    Msg.READ + " SHORT DEFAULT 0," +
    Msg.FLASH + " SHORT DEFAULT 0" +
");");

db.execSQL("CREATE VIRTUAL TABLE " + MsgText._TABLE_NAME + " USING FTS3 (" + 
    MsgText._ID + PRIMARY_KEY +
    MsgText.TITLE + " TEXT," +
    MsgText.CONTENT + " TEXT," +
    MsgText.KEYWORDS + " TEXT," +
    "FOREIGN KEY(" + MsgText._ID + ") " +
    "REFERENCES " + Msg._TABLE_NAME + "(" + Msg._ID + ") " +
");");

Then I created View to use by queries:

db.execSQL("CREATE VIEW IF NOT EXISTS " + View.MSG_CONTENT +
    " AS SELECT " +
    Msg._TABLE_NAME + "." + Msg._ID + ", " +
    Msg._TABLE_NAME + "." + Msg.READ + ", " +
    Msg._TABLE_NAME + "." + Msg.FLASH + ", " +
(...)
    MsgText._TABLE_NAME + "." + MsgText.TITLE + ", " +
    MsgText._TABLE_NAME + "." + MsgText.CONTENT +
    " FROM " + Msg._TABLE_NAME + ", " + MsgText._TABLE_NAME +
    " WHERE " + Msg._TABLE_NAME + "." + Msg._ID + "=" +
    MsgText._TABLE_NAME + "." + MsgText._ID);

This works very well for me as I can query data using parameters and do fulltext search when needed. Query performance is the same as when using just one table.

I hope this helps someone else who might bump into the same issue.

Cheers,
PeS

P.S. Checked Meta and it is OK to reply to self, apparently.



来源:https://stackoverflow.com/questions/4847785/android-sqlite-query-not-binding-integer-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!