SQLite的和Android。如何使用字符串有我的查询?我的、字符串、如何使用、SQLite

2023-09-08 09:49:49 作者:夜染°

我有一个拉一切从数据库到一个列表视图的查询。

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

返回mDb.query(DATABASE_TABLE,新的String [] {KEY_ROWID,KEY_HEIGHT,                     KEY_BODY,KEY_HOMEID},NULL,NULL,NULL,NULL,NULL,NULL);

return 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.

我想只有在该领域KEY_HOMEID一个名为journalId变量相匹配的记录,这应该是一个数字串。

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

我也想知道如何把可变进我DatabaseAdapter ... 将((资源)this.getApplication())。getjournalId())工作?资源是可扩展的应用程序的类。

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.

其次,请阅读 SQLiteDatabase.query的文档() 。 SQL关键字,比如从和具有不应该构成方法调用的一部分。

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

您已经通过了表名作为第一个参数查询(),所以写从DATABASE_NAME WHERE ... 既是冗余的,会导致形成一个无效的查询。

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.

第三,你不能在Java中的变量名传为选择参数字符串&mdash的一部分;你需要有这样一个字符串: KEY_HOMEID +=+ journalId

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.

第四,它通常是一个好主意,用 selectionArgs两个,而不是嵌入的查询参数到选择。尤其是当你有字符串或参数,用户输入的到来。 例如选择= KEY_HOMEID +=? selectionArgs两个=新的String [] {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);