我的光标返回记录两次,即使我将distinct设置为true:
返回myDataBase.query(true,DB_TABLE,new String [] {"rowid as _id",KEY_CONDITIONS},builder.toString(),symptoms,null,null,null,null);
仅供参考,
public Cursor getData(String[] symptoms) {
String where = KEY_SYMPTOMS + "= ?";
String orStr = " OR ";
StringBuilder builder = new StringBuilder(where);
for(int i = 1; i < symptoms.length; i++)
builder.append(orStr).append(where);
return myDataBase.query(true, DB_TABLE, new String[]
{"rowid as _id", KEY_CONDITIONS}, builder.toString(), symptoms, null, null, null, null);
}
Run Code Online (Sandbox Code Playgroud)
或者我尝试更改为rawQuery
return myDataBase.rawQuery("SELECT DISTINCT " + KEY_CONDITIONS + " FROM "
+ DB_TABLE + " " + builder.toString() + symptoms.toString(), null);
Run Code Online (Sandbox Code Playgroud)
我的LogCat说:
03-02 22:57:02.634: E/AndroidRuntime(333): FATAL EXCEPTION: main
03-02 22:57:02.634: E/AndroidRuntime(333): android.database.sqlite.SQLiteException: near "=": syntax error: , while compiling: SELECT DISTINCT conditions FROM tblSymptoms symptoms= ? OR symptoms= ?[Ljava.lang.String;@405550f8
Run Code Online (Sandbox Code Playgroud)
请帮我确定一下这里似乎缺少什么.真的很感激任何帮助.

解决方案
您需要DISTINCT条件,但Android需要_id列是一个问题,因为您不能混合和匹配:.但是,您可以使用该SELECT _id, DISTINCT condition...GROUP BY子句:
return myDataBase.query(DB_TABLE, new String[] {"rowid as _id", KEY_CONDITIONS},
builder.toString(), symptoms, KEY_CONDITIONS, null, null);
Run Code Online (Sandbox Code Playgroud)
说明
此查询:
return myDataBase.rawQuery("SELECT DISTINCT " + KEY_CONDITIONS + " FROM "
+ DB_TABLE + " " + builder.toString() + symptoms.toString(), null);
Run Code Online (Sandbox Code Playgroud)
因为传入String[] symptoms错误的参数而失败,请尝试:
return myDataBase.rawQuery("SELECT DISTINCT " + KEY_CONDITIONS + " FROM "
+ DB_TABLE + " " + builder.toString(), symptoms);
Run Code Online (Sandbox Code Playgroud)
这个查询:
return myDataBase.query(true, DB_TABLE, new String[] {"rowid as _id", KEY_CONDITIONS}, builder.toString(), symptoms, null, null, null, null);
Run Code Online (Sandbox Code Playgroud)
失败,因为DISTINCT在看这两个 ID和状态栏.它相当于:SELECT DISTINCT(_id, conditions) ...你显然只想要不同的条件......
| 归档时间: |
|
| 查看次数: |
3761 次 |
| 最近记录: |