Sqlite Multiple Where子句语法错误

时间:2018-03-08 09:37:29

标签: android android-sqlite

我正在尝试实现一个SQLite查询,我必须使用多个'where'子句 我的代码是这样的,

public boolean CheckForOption1(String ans1,String ans2,String ans3,String ans4) {
    SQLiteDatabase db = this.getReadableDatabase();
    String Query = "SELECT * FROM " + TABLE_NAME + " WHERE " + ANSWER1 + " = '"+ans1+"'"+", "+ ANSWER2 + " ='"+ans2+"' "+", "+ ANSWER3 + " ='"+ans3+"'"+", "+ ANSWER4 + " ='"+ans4+"' ";
    Cursor cursor = db.rawQuery(Query, null);
    if(cursor.getCount() <= 0){
        cursor.close();
        return false;
    }
    cursor.close();
    return true;
}

我在我的活动中这样称呼它:

boolean isTrue = sqLiteHandler.CheckForOption1("Fixed Angle","up to 6","Up to 4000rpm","No");
    if (isTrue){
        Toast.makeText(this, "yes its true", Toast.LENGTH_SHORT).show();
    }

但我得到的错误是这样的:

Caused by: android.database.sqlite.SQLiteException: near ",": syntax error   (code 1): , while compiling: SELECT * FROM table1 WHERE answer1 = 'Fixed Angle', answer2 ='up to 6' , answer3 ='Up to 4000rpm', answer4 ='No'

我做错了什么?

2 个答案:

答案 0 :(得分:1)

您需要在where子句中使用AND而不是,

public boolean CheckForOption1(String ans1,String ans2,String ans3,String ans4) {
    SQLiteDatabase db = this.getReadableDatabase();
    String Query = "SELECT * FROM " + TABLE_NAME + " WHERE " + ANSWER1 + " = '"+ans1+"'"+" AND "+ ANSWER2 + " ='"+ans2+"' "+" AND "+ ANSWER3 + " = '"+ans3+"'"+" AND "+ ANSWER4 + " ='"+ans4+"' ";
    Cursor cursor = db.rawQuery(Query, null);
    if(cursor.getCount() <= 0){
        cursor.close();
        return false;
    }
    cursor.close();
    return true;
}

答案 1 :(得分:0)

AND替换为String Query = "SELECT * FROM " + TABLE_NAME + " WHERE " + ANSWER1 + " = '"+ans1+"'"+" AND "+ ANSWER2 + " ='"+ans2+"' "+" AND "+ ANSWER3 + " ='"+ans3+"'"+" AND "+ ANSWER4 + " ='"+ans4+"' "; 以分隔您的条件

{{1}}