Android使用数据库检查是否存在2条记录

时间:2018-07-04 04:36:26

标签: android sqlite android-sqlite

当我检查用户名时,我有一个可以正常工作的函数。当我检查密码时,它总是错误的。

public boolean checkIfUser(String userName, String password){

        //validate user if the user exists return true.
        SQLiteDatabase db = this.getReadableDatabase();
        String query ="select * from user_table where user_name = "+"\""+userName+"\"";
        //String query ="select * from user_table where password = "+"\""+password+"\"";

        Cursor cursor = db.rawQuery(query,null);
        if(cursor.getCount() <=0){
            return  false;
        }
        return true;

    }

要检查密码,我将字符串更改为

   String query ="select * from user_table where password = "+"\""+ password +"\"";

有什么主意会导致此错误吗?

3 个答案:

答案 0 :(得分:0)

尝试使用

String query ="select * from user_table where user_name = "'"+userName+"'"; String query ="select * from user_table where password = "'"+password+"'";

这可能会帮助

答案 1 :(得分:0)

尝试一下

SELECT * FROM " + Table_NAME + " WHERE (" + COL2_PASSWORD + " = \'" + password + "\')";

答案 2 :(得分:0)

您应该尝试像这样验证您的登录信息:

String[] selectionArg = new String[]{userName, password};
String query ="select * from user_table where user_name = ? and password = ?";
Cursor cursor = db.rawQuery(query,selectionArg);
 if(cursor.getCount() <=0){
    return  false;
}
return true;