开发者

SQLite and Android. How do I use String Having in my query?

I have a query that pulls everything from a database into a list view.

re开发者_如何学Goturn mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_HEIGHT, KEY_BODY, KEY_HOMEID},null , null, null, null, null,null);

                                |               String Having is the 3rd from
                                                           last null.

I want only the records where the field KEY_HOMEID matches a variable called journalId which should be a numeric string.

I would also like to know how to bring the variable into my DatabaseAdapter... would ((resource) this.getApplication()).getjournalId()) work?? resource is a class that extends application.


Firstly, posting error messages rather than "no dice" would be helpful.

Secondly, please read the documentation for SQLiteDatabase.query(). SQL keywords like "FROM" and "HAVING" shouldn't form part of the method call.

You've already passed the table name as the first parameter to query(), so writing "FROM DATABASE_NAME WHERE ..." is both redundant and will lead to an invalid query being formed.

Thirdly, you can't pass in Java variable names as part of the selection parameter String — you need to have a String like: KEY_HOMEID +"="+ journalId.

Fourthly, it's generally a good idea to use selectionArgs rather than embedding the query parameters into the selection. Especially when you have Strings or parameters coming from user input.
e.g. selection = KEY_HOMEID +"=?" and selectionArgs = new String[] { journalId }

Overall, your method call should look like:

mDb.query(DATABASE_TABLE, 
          new String[] { KEY_ROWID, KEY_HEIGHT, 
                         KEY_BODY, KEY_HOMEID },
          KEY_HOMEID +"=?", new String[] { journalId },
          null, null, null);


Use a WHERE clause; HAVING is only used with GROUP BY.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜