Android SQL查询多个WHERE args

时间:2014-07-02 07:30:56

标签: android android-sqlite where-in

出于某种原因,我的sql.query由于其格式化方式而失败。我不知道为什么。它不会崩溃也不会引发错误。它根本不返回值。我在查询中使用两个WHERE参数。我在Log.d中看到两个参数都成功传递给方法。

public static String fetchLightTableD(String flightRowId, String filenamePrefix){

    String[] lightcolumns = new String[] { KEY_ROWID, KEY_TID, KEY_NAME, KEY_LUXVALUE, KEY_TRANSMITTED };
    Log.d("CSV", "SQL fetchLightTableD, revceived tid - " + filenamePrefix + " " + flightRowId);
    Cursor lightcursor = myDatabase.query(LIGHT_TABLE, lightcolumns, KEY_TID + "=" + filenamePrefix + " AND " + KEY_ROWID + "=" + flightRowId, null, null, null, null);
    String lightTableResults = "";

    int iRowId = lightcursor.getColumnIndex(KEY_ROWID);
    int iTid = lightcursor.getColumnIndex(KEY_TID);
    int iName = lightcursor.getColumnIndex(KEY_NAME);
    int iLux = lightcursor.getColumnIndex(KEY_LUXVALUE);
    int iTrans = lightcursor.getColumnIndex(KEY_TRANSMITTED);

    for (lightcursor.moveToFirst(); !lightcursor.isAfterLast(); lightcursor
            .moveToNext()) {
        lightTableResults = lightTableResults
                + lightcursor.getString(iRowId) + ","
                + lightcursor.getString(iTid) + ","
                + lightcursor.getString(iName) + ","
                + lightcursor.getString(iLux) + ","
                + lightcursor.getString(iTrans) + "\n";


    }

    return lightTableResults;

}

它应该过滤表中与rowid和filenameprefix匹配的行。我还测试了删除一个或另一个WHERE args以确保实际存在的值。当然他们这样做,所以我的AND有问题。任何人都可以看到它的错误吗?

等价物应如下:

SELECT lightcolumns 
FROM LIGHT_TABLE 
WHERE KEY_TID=rowid AND KEY_TID=filenamePrefix;

2 个答案:

答案 0 :(得分:3)

像这样使用您的查询

 Cursor lightcursor = myDatabase.query(LIGHT_TABLE,lightcolumns,
            KEY_TID "=?"+ " AND " + KEY_ROWID +"=?",
            new String[] { filenamePrefix , flightRowId},null, null, null);

而不是

Cursor lightcursor = myDatabase.query(LIGHT_TABLE, lightcolumns, KEY_TID + "=" + filenamePrefix + " AND " + KEY_ROWID + "=" + flightRowId, null, null, null, null);

答案 1 :(得分:0)

试试这个

String Query = "SELECT " + lightcolumns + " FROM "+ LIGHT_TABLE + " WHERE " + KEY_TID + " = "+ rowid + " AND " + KEY_TID + " = " +filenamePrefix ; 

Cursor lightcursor = myDatabase.rawquery(Query ,null);