![](/img/trans.png)
[英]How to search for a specific filename in the sdcard by android ContentResolver.query using selection and selectionArgs?
[英]selectionArgs in ContentResolver.query(…) can be a subquery?
我在Android上使用SQLite。
我使用ContentProvider
从db查询数据。 现在,尝试通过ContentResolver
使用子查询时遇到问题
String selection = "cat_id NOT IN ?"
String[] selectionArgs = new String[]{"(SELECT Categories.id FROM Categories)"}
cursor = mResolver.query(getContentUri(), getListColumns(),
selection, selectionArgs, orderBy);
这是错误:
08-06 10:32:36.070: E/AndroidRuntime(2151): Caused by: android.database.sqlite.SQLiteException: near "?": syntax error (code 1): , while compiling: SELECT * FROM TRANSACTIONS WHERE cat_id NOT IN ? ORDER BY time_created ASC, id ASC`
我的问题是“我可以使用selectionArgs作为子查询吗?”
我的目的是“获取cat_id不在类别表中的事务列表”。
谁能帮我?
别忘记了吗? 实际上是一个值的占位符。 以后哪个将被android绑定。
因此,您不会将查询替换为?。 作为使用点? 是确保sql代码不是由用户参数注入的。
String selection = "cat_id NOT IN ?"
String[] selectionArgs = new String[]{"(SELECT Categories.id FROM Categories)"}
cursor = mResolver.query(getContentUri(), getListColumns(), selection, selectionArgs, orderBy);
和你一样写作就像
String selection = "cat_id NOT IN '(SELECT Categories.id FROM Categories)'"
你想要运行的查询实际上是作为一个值,这意味着NOT IN '(some value)'
不是有效的sql。
我建议你删除? 并将其替换为您将在其中修复它的where参数中的查询。
你会用的吗? 像这样的占位符(如果你知道它不必是什么)。
String selection = "cat_id NOT IN (?, ?)"
String[] selectionArgs = new String[]{"value1", "value2"}
cursor = mResolver.query(getContentUri(), getListColumns(), selection, selectionArgs, orderBy);
编辑:试试
String selection = "cat_id NOT IN (SELECT Categories.id FROM Categories)"
cursor = mResolver.query(getContentUri(), getListColumns(), selection, null, orderBy);
我给你一个应该工作的例子(在我的情况下更新ContentResolver方法):
String selection = DatabaseContract.EventTable.Column.ID_EVENT + " IN (SELECT DISTINCT "
+ DatabaseContract.CountryEventTable.Column.EVENT_ID + " FROM "
+ DatabaseContract.CountryEventTable.TABLE_NAME + " WHERE "
+ DatabaseContract.CountryEventTable.Column.COUNTRY_CODE + "=?)";
String [] selectionArgs = new String[]{
data[0],
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.