没有这样的列sqlite异常

时间:2014-12-17 19:54:28

标签: android sqlite android-sqlite

我正在尝试从用户个人资料中获取一些数据,但它找不到该列。我已经检查了每个查询之间的空间,似乎没有错。直到现在我看不出问题,任何想法?谢谢你 这是我的代码和错误消息

表名

private static final String TABLE_PROFILE = "users"; 
private static final String TABLE_STATUS = "status";

用户列名

//Users column names
private static final String KEY_USER_ID = "user_id";
private static final String KEY_USER_IMG = "picture";
private static final String KEY_USER_EMAIL = "email";
private static final String KEY_USER_NAME = "name";
private static final String KEY_USER_PASSWORD = "password";
private static final String KEY_USER_PHONE = "phone";

状态列名称

//Status column names
private static final String KEY_STATUS_ID = "status_id";
private static final String KEY_STATUS = "status";

表创建声明

 //Users table create statement
private static final String CREATE_TABLE_PROFILE = "CREATE TABLE " + TABLE_PROFILE + 
        "(" + KEY_USER_ID + " INTEGER PRIMARY KEY," + KEY_USER_IMG + " BLOB," + 
        KEY_USER_EMAIL + " VARCHAR," + KEY_USER_NAME + " VARCHAR," + KEY_USER_PASSWORD + " VARCHAR," 
        + KEY_USER_PHONE + " VARCHAR," + KEY_CREATED_AT + " DATETIME," + KEY_STATUS_ID + " INTEGER," 
        + " FOREIGN KEY " + "(" + KEY_STATUS_ID + ")" + " REFERENCES " + TABLE_STATUS + "(" + KEY_STATUS_ID + ")" + ")";

getProfile方法

public Profile getProfile(String username){
    SQLiteDatabase db = this.getReadableDatabase();
    String selectQuery = "SELECT * FROM " + TABLE_PROFILE + " WHERE " + KEY_USER_EMAIL + " = " + username;
    Log.e(LOG, selectQuery);
    Cursor c = db.rawQuery(selectQuery, null);

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

    Profile pf = new Profile();
    pf.setID(c.getInt(c.getColumnIndex(KEY_USER_ID)));
    pf.setImg(c.getBlob(c.getColumnIndex(KEY_USER_IMG)));
    pf.setEmail(c.getString(c.getColumnIndex(KEY_USER_EMAIL)));
    pf.setName(c.getString(c.getColumnIndex(KEY_USER_NAME)));
    pf.setPassword(c.getString(c.getColumnIndex(KEY_USER_PASSWORD)));
    pf.setPhone(c.getString(c.getColumnIndex(KEY_USER_PHONE)));
    pf.setCreatedAt(c.getString(c.getColumnIndex(KEY_CREATED_AT)));

    return pf;
}

logcat消息

12-18 02:50:32.616: E/DatabaseHelper(7093): SELECT * FROM users WHERE email = test1
12-18 02:50:32.616: E/SQLiteLog(7093): (1) no such column: test1
12-18 02:50:32.636: E/AndroidRuntime(7093): FATAL EXCEPTION: main
12-18 02:50:32.636: E/AndroidRuntime(7093): android.database.sqlite.SQLiteException: no such column: test1 (code 1): , while compiling: SELECT * FROM users WHERE email = test1

3 个答案:

答案 0 :(得分:1)

在SQL中,必须引用字符串文字。

但是,为避免格式化此类问题(以及SQL注入攻击),请改用parameters

Cursor c = db.rawQuery("SELECT * FROM " + TABLE_PROFILE +
                       " WHERE " + KEY_USER_EMAIL + " = ?",
                       new String[]{ username });

答案 1 :(得分:0)

KEY_USER_EMAIL列不是字符串类型吗?如果是这样的话,你的where子句WHERE" + KEY_USER_EMAIL +" =" +用户名 试图将列值称为KEY_USER_EMAIL = test1,而它应该真正引用/使用它,

KEY_USER_EMAIL =' test1' 只需将您的Where子句更改为这样 -

SELECT * FROM " + TABLE_PROFILE + " WHERE " + KEY_USER_EMAIL + " = '" + username + "'"

答案 2 :(得分:0)

使用此

String selectQuery = "SELECT * FROM " + TABLE_PROFILE + " WHERE " + KEY_USER_EMAIL + " = " + "'" + username + "'";

用户名的值需要作为字符串传递。