在sqlite中检查有效的用户名和密码时获取异常(getting exception while checking for valid username and password in sqlite)

我正在尝试检查有效用户。 但我得到以下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; }

我究竟做错了什么?? 请帮忙

I'm trying to check for a valid user. But I'm getting the following Exception:

no such column: ABC(Code 1):, while compiling: SELECT _id, username, password FROM MyTable1 WHERE username=ABC

The code is as follows:

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; }

What am I doing wrong?? Please help

最满意答案

如果用户名是varchar / text类型,那么

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; }

If username is varchar/text type then

Cursor cursor = database.query(TABLE_NAME, allColumns, COLUMN_USER_NAME + "=" + '"+userName+"', null, null, null, null);

This is sample login function

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){ } }

or

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; }在sqlite中检查有效的用户名和密码时获取异常(getting exception while checking for valid username and password in sqlite)

我正在尝试检查有效用户。 但我得到以下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; }

我究竟做错了什么?? 请帮忙

I'm trying to check for a valid user. But I'm getting the following Exception:

no such column: ABC(Code 1):, while compiling: SELECT _id, username, password FROM MyTable1 WHERE username=ABC

The code is as follows:

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; }

What am I doing wrong?? Please help

最满意答案

如果用户名是varchar / text类型,那么

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; }

If username is varchar/text type then

Cursor cursor = database.query(TABLE_NAME, allColumns, COLUMN_USER_NAME + "=" + '"+userName+"', null, null, null, null);

This is sample login function

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){ } }

or

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; }