Android 新的 Sqlite 语法错误 "foo"

Android Sqlite Syntax error new "foo"

我已经为此检查了 Whosebug,但我仍然没有找到适合我的答案。我试图通过 sqlite 创建用户登录,但我不断从 logcat、

收到此错误

03-11 16:19:57.767: E/com.asmaushaheedah.database.DatabaseHelper(25118): SELECT id FROM user WHERE email & password =?foo@example.com hello 03-11 16:19:57.767: E/SQLiteLog(25118): (1) near "foo": syntax error

我的数据库助手 class 的一个片段是:

public class DatabaseHelper extends SQLiteOpenHelper {

private static final String LOG = DatabaseHelper.class.getName();

private static final int DATABASE_VERSION = 1;


private static final String DATABASE_NAME = "projectManager";

// Table Names
private static final String TABLE_USER = "user";

private static final String KEY_ID = "id";

// USER Table - column nmaes
private static final String KEY_NAME = "name";
private static final String KEY_SURNAME = "surname";
private static final String KEY_DOB = "dob";
private static final String KEY_NICK = "nick";
private static final String KEY_EMAIL = "email";
private static final String KEY_PASSWORD = "password";


private static final String CREATE_TABLE_USER = "CREATE TABLE "
        + TABLE_USER + "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_NAME
        + " text," + KEY_SURNAME + " text," + KEY_DOB + " DATETIME,"
        + KEY_NICK + " text," + KEY_EMAIL + " text," + KEY_PASSWORD
        + " text" + ")";

 public User getUser(String email, String password) {
    SQLiteDatabase db = this.getReadableDatabase();


    String selectQuery = "SELECT id FROM " + TABLE_USER + " WHERE "
            + KEY_EMAIL + " & " + KEY_PASSWORD+ " =?" + email + password;

    Log.e(LOG, selectQuery);

    Cursor c = db.rawQuery(selectQuery, null);

    if (c != null)
        c.moveToFirst();

    User td = new User();
    td.setID(c.getInt(c.getColumnIndex(KEY_ID)));
    td.setName((c.getString(c.getColumnIndex(KEY_NAME))));
    td.setSurname(c.getString(c.getColumnIndex(KEY_SURNAME)));
    td.setDob((c.getString(c.getColumnIndex(KEY_DOB))));
    td.setNick(c.getString(c.getColumnIndex(KEY_NICK)));
    td.setEmail(c.getString(c.getColumnIndex(KEY_EMAIL)));
    td.setPassword(c.getString(c.getColumnIndex(KEY_PASSWORD)));
    return td;
}

我不确定使用 sqlite 进行用户登录是否是最佳选择,如果不是,谁能提出更好的选择。

替换此代码:

String selectQuery = "SELECT id FROM " + TABLE_USER + " WHERE "
            + KEY_EMAIL + " & " + KEY_PASSWORD+ " =?" + email + password;

Log.e(LOG, selectQuery);

Cursor c = db.rawQuery(selectQuery, null);

通过这个:

String[] columns = {KEY_ID};
String where = KEY_EMAIL+"=? AND " + KEY_PASSWORD + "=?";
String[] whereArgs = {email, password};
Cursor cursor = db.query(TABLE_USER, 
                columns, 
                where, whereArgs, null, null, null);