3

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.

PeS
  • 3,757
  • 3
  • 40
  • 51

1 Answers1

3

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.

Community
  • 1
  • 1
PeS
  • 3,757
  • 3
  • 40
  • 51
  • Not only OK, it's actually encouraged - if no one comes up with a solution, and you find one, kudos for sharing. – Phil Lello Nov 10 '11 at 23:27