在Android中查询Sqlite数据库使用子句

时间:2014-02-14 11:18:51

标签: android sqlite android-sqlite

我在下面的查询中收到错误。

错误是它找不到类型字符串。

我的查询有什么问题?

String selectQuery = "SELECT  * FROM " + TABLE_CONTACTS + " WHERE " + KEY_TYPE + " ='" + type + "'";

我的日志猫 我的Log Cat 02-14 16:33:17.925:E / AndroidRuntime(25839):java.lang.RuntimeException:无法启动活动ComponentInfo android.database.sqlite.SQLiteException:没有这样的列:type :, while compiling:SELECT *来自Mycontacts WHERE type ='whitelist'

完整查询代码

public List<Contacts> getAllphContacts(String type) {
        List<Contacts> contactList = new ArrayList<Contacts>();
        // Select All Query

        //String selectQuery =  "SELECT  * FROM " + TABLE_CONTACTS +KEY_TYPE + "=?";                //*tried this also  
        String selectQuery = "SELECT * FROM " + TABLE_CONTACTS + " WHERE " + KEY_TYPE + " ="+ type; //***this also not working
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Contacts contact = new Contacts();
                contact.setphid(Integer.parseInt(cursor.getString(0)));             
                contact.setphname(cursor.getString(1));
                contact.setphnumber(cursor.getString(2));
                contact.settype(cursor.getString(3));//********For whiteList*********//

                // Adding contact to list
              contactList.add(contact);
            } while (cursor.moveToNext());
        }
        cursor.close();
        db.close();
        // return contact list
        return contactList;
    }

1 个答案:

答案 0 :(得分:-1)

    String QUERY = "SELECT * FROM Cart Where id=" + id";
cursor = data.selectQuery(QUERY);
int count = cursor.getCount();

if (count <= 0) {
/// insert
}else{

/// update
}