Android:我的查询出了什么问题?

时间:2014-05-25 14:01:49

标签: android sqlite

DataBaseAdapter方法:

public long insertRecordCategory(String name, String device)
    {
        ContentValues cv = new ContentValues();
        cv.put(NAME, name);
        cv.put(DEVICE, device);

        if(!updateRecord(cv, device))
                return db.insert(TABLE, null, cv);
        else return 0;
    }

    private boolean updateRecord(ContentValues values, String device) {
        return db.update(TABLE, values, DEVICE + "=" + device, null) > 0;
    }

    public String isDeviceRegistered(String device) {
        String [] coloane = {NAME, DEVICE};
        Cursor c = db.query(true, TABLE, coloane, DEVICE + "=" + device, null, null, null, null, null);
        if(c == null)
            return null;
        c.moveToFirst();
        return c.getString(c.getColumnIndex(NAME));
    }

当我试图打电话时:isDeviceRegistered它说:

05-25 16:44:47.170: E/AndroidRuntime(14504): android.database.sqlite.SQLiteException: near ":02": syntax error (code 1): , while compiling: SELECT DISTINCT _name, _device FROM FriendsTable WHERE _device=22:02:af:db:24:9a

调用更新时,它说:

05-25 17:36:55.190: E/AndroidRuntime(469): android.database.sqlite.SQLiteException: near ":02": syntax error (code 1): , while compiling: UPDATE FriendsTable SET device=?,name=? WHERE device=22:02:af:db:24:9a

此更新方法与其中有效的另一个项目相同。

2 个答案:

答案 0 :(得分:2)

SQL中的字符串文字必须位于'single quotes'中。但是,最好使用bind args。取代

DEVICE + "=" + device, null

DEVICE + "=?", new String[] { device }

答案 1 :(得分:0)

您错过了'占位符。

替换

DEVICE + "=" + device

DEVICE + "='" + device + "'"