在android中的SELECT语句中传递值数组

时间:2017-01-10 23:41:52

标签: android sqlite

我有一个select语句,根据用户动态生成的症状在sqlite数据库中选择疾病...这是查询: SELECT D."+COLUMN_NAME+",group_concat( symp,',' ) AS conca,count(*) as SymptomsMatching FROM "+TABLE_LINKDS+ " DS JOIN "+TABLE_SYMPTOMS+" S ON DS."+COLUMN_SYMPTOMS_ID+"= S."+COLUMN_SYM_ID+ " JOIN "+TABLE_DISEASE+" D ON DS."+COLUMN_DISEASE_ID+"= D."+COLUMN_DS_ID+ " WHERE S."+COLUMN_SYMP+" IN ("+arrayOfSymptoms+") GROUP BY D.name ORDER BY SymptomsMatching DESC, D.name ASC";

现在我想在查询中传递数组:String[] arrayOfSymptoms={"headache","nausea","chills"},使查询看起来像这样:

"SELECT D."+COLUMN_NAME+",group_concat( symp,',' ) AS conca,count(*) as SymptomsMatching FROM "+TABLE_LINKDS+
            " DS JOIN "+TABLE_SYMPTOMS+" S ON DS."+COLUMN_SYMPTOMS_ID+"= S."+COLUMN_SYM_ID+
            " JOIN "+TABLE_DISEASE+" D ON DS."+COLUMN_DISEASE_ID+"= D."+COLUMN_DS_ID+
            " WHERE S."+COLUMN_SYMP+" IN ('headache','nausea','chills') GROUP BY D.name ORDER BY SymptomsMatching DESC, D.name ASC";

我尝试使用TextUtils.join()方法和另一个自定义buh所有dint工作..有人可以请帮忙..谢谢

3 个答案:

答案 0 :(得分:0)

您可以使用此功能:

SELECT COUNT(r.id) AS total, CONCAT(m1.first_name,' ', m1.last_name) AS fromName, 
(SELECT COUNT(id) FROM Referrals WHERE rtype=1 AND from_id=r.from_id GROUP BY fromName) AS external,
(SELECT COUNT(id) FROM Referrals WHERE rtype=2 AND from_id=r.from_id GROUP BY fromName) AS internal
FROM Referrals AS r JOIN Members AS m1 ON m1.id=r.from_id WHERE QUARTER(rdate) = @LASTQTR AND YEAR(rdate) = @YR  GROUP BY fromName ORDER BY total DESC LIMIT 10

然后在SQL字符串中调用它。

答案 1 :(得分:0)

像这样的东西

public static String fromArray(String[] in) {
    StringBuilder result = new StringBuilder();
    for (int i = 0; i < in.length - 1; i++) {
        if (i != 0) {
            result.append(",");
        }
        result.append("'" + in[i] + "'");
    }
    return result.toString();
}

答案 2 :(得分:-1)

您可以在数组上使用简单的toString()方法,并删除前一个和尾随。

检查此SO链接(我认为回答类似的问题) -

Android - sqlite in clause using values from array