[英]selectionArgs in ContentResolver.query(…) can be a subquery?
I'm working with SQLite on Android. 我在Android上使用SQLite。
I used ContentProvider
to query data from db. 我使用
ContentProvider
从db查询数据。 And now, I have a problem when try to use subquery via ContentResolver
现在,尝试通过
ContentResolver
使用子查询时遇到问题
String selection = "cat_id NOT IN ?"
String[] selectionArgs = new String[]{"(SELECT Categories.id FROM Categories)"}
cursor = mResolver.query(getContentUri(), getListColumns(),
selection, selectionArgs, orderBy);
And this is the error: 这是错误:
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`
My question is "Can I use selectionArgs be a Subquery?" 我的问题是“我可以使用selectionArgs作为子查询吗?”
My purpose is "get the list of transactions where cat_id is NOT IN a Category table". 我的目的是“获取cat_id不在类别表中的事务列表”。
Who can help me? 谁能帮我?
Don't forget here that ? 别忘记了吗? is actually a placeholder for a value.
实际上是一个值的占位符。 Which will be binded later by android.
以后哪个将被android绑定。
As such, you would not put a query to be replaced by ?. 因此,您不会将查询替换为?。 As the point of using the ?
作为使用点? is to ensure that sql code is not injected by user parameters.
是确保sql代码不是由用户参数注入的。
String selection = "cat_id NOT IN ?"
String[] selectionArgs = new String[]{"(SELECT Categories.id FROM Categories)"}
cursor = mResolver.query(getContentUri(), getListColumns(), selection, selectionArgs, orderBy);
Is equilivant to you writing something like 和你一样写作就像
String selection = "cat_id NOT IN '(SELECT Categories.id FROM Categories)'"
Where the query you want run is actually being though of as a value, meaning that NOT IN '(some value)'
is not valid sql. 你想要运行的查询实际上是作为一个值,这意味着
NOT IN '(some value)'
不是有效的sql。
I suggest that you just remove the ? 我建议你删除? and replace it with the query you have in your where arguments which will fix it.
并将其替换为您将在其中修复它的where参数中的查询。
You would use the ? 你会用的吗? placeholders like this (if you knew what it didn't have to be in).
像这样的占位符(如果你知道它不必是什么)。
String selection = "cat_id NOT IN (?, ?)"
String[] selectionArgs = new String[]{"value1", "value2"}
cursor = mResolver.query(getContentUri(), getListColumns(), selection, selectionArgs, orderBy);
Edit: Try 编辑:试试
String selection = "cat_id NOT IN (SELECT Categories.id FROM Categories)"
cursor = mResolver.query(getContentUri(), getListColumns(), selection, null, orderBy);
I give you an example that should work (in update ContentResolver method in my case): 我给你一个应该工作的例子(在我的情况下更新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.