Nam*_*mLe 5 sqlite android android-contentprovider
我在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);
Run Code Online (Sandbox Code Playgroud)
这是错误:
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`
Run Code Online (Sandbox Code Playgroud)
我的问题是"我可以使用selectionArgs作为子查询吗?"
我的目的是"获取cat_id不在类别表中的事务列表".
谁能帮我?
Nic*_*las 11
别忘记了吗?实际上是一个值的占位符.以后哪个将被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);
Run Code Online (Sandbox Code Playgroud)
和你一样写作就像
String selection = "cat_id NOT IN '(SELECT Categories.id FROM Categories)'"
Run Code Online (Sandbox Code Playgroud)
你想要运行的查询实际上是作为一个值,这意味着
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);
Run Code Online (Sandbox Code Playgroud)
编辑:试试
String selection = "cat_id NOT IN (SELECT Categories.id FROM Categories)"
cursor = mResolver.query(getContentUri(), getListColumns(), selection, null, orderBy);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17811 次 |
| 最近记录: |