我正在尝试检查有效用户。但我得到以下Exception
:
no such column: ABC(Code 1):, while compiling: SELECT _id, username, password
FROM MyTable1 WHERE username=ABC
代码如下:
public int checkUser(String userName, String password) {
int check = -1;
int id = 2;
String psword = new String();
Cursor cursor = database.query(TABLE_NAME, allColumns, COLUMN_USER_NAME + "=" + userName, null, null, null, null);
cursor.moveToFirst();
Log.v(TAG, "After CURSOR!!");
psword = cursor.getString(cursor.getColumnIndex(COLUMN_PASSWORD));
Log.v(TAG, "pasword: " + psword);
if (psword.equals(password)) {
check = 1;
}
return check;
}
我做错了什么?请帮忙
答案 0 :(得分:0)
如果用户名是varchar / text type,那么
Cursor cursor = database.query(TABLE_NAME, allColumns, COLUMN_USER_NAME
+ "=" + '"+userName+"', null, null, null, null);
这是示例登录功能
public boolean checkUser(String username, String password) throws SQLException
{
String whereClause = " USERNAME = '"+userName+"' ";
try {
open();// dbopen
Cursor cursor = db.query(TABLE_NAME, allColumns,whereClause, null, null, null, null);
}catch(Exception e){
}
}
或
public boolean checkUser(String username, String password) throws SQLException
{
Cursor mCursor = database.rawQuery("SELECT * FROM " + DATABASE_TABLE + " WHERE username=? AND password=?", new String[]{username,password});
if (mCursor != null) {
if(mCursor.getCount() > 0)
{
return true;
}
}
return false;
}
答案 1 :(得分:0)
您应该在userName周围添加引号。您想要与字符串值“ABC”或“ABC”匹配的行我不记得哪些引号。如果您不放置引号,则与名为ABC的列匹配。
您应该使用prepares语句。这样可以避免许多问题。