如何使用String变量而不是Integer在SQLite数据库中搜索?

tha*_*rif 1 sqlite android

我的部分应用程序是从动态数据库中提取用户帐户信息.当用户输入他的用户名和密码时,我将在数据库中搜索用户名并带来与其用户名相关的所有数据.我使用getTwitterAccount进行搜索,我的主键是字符串"TW_USERNAME".这是数据库代码的一部分:

public String[] getTwitterAccount(String username) {
    Cursor cursor = db.query(true, TABLE_1, null, TW_USERNAME + "=" + username, null, null, null, null, null);


    if ((cursor.getCount() == 0) || !cursor.moveToFirst()) {
        return null;
    }

    String[] account = new String[3]; 
    account[0] = cursor.getString(cursor.getColumnIndex(TW_USERNAME));
    account[1] = cursor.getString(cursor.getColumnIndex(TW_AUTH_KEY));
    account[2] = cursor.getString(cursor.getColumnIndex(TW_AUTH_SECRET_KEY));

    return account;
}  
Run Code Online (Sandbox Code Playgroud)

我在查询中遇到错误.我搜索了这个问题,他们说"主键应该是整数"_id".无论如何使用String not Integer在数据库中搜索?

kos*_*osa 5

试试这个:TW_USERNAME +"='"+ username +"'"你的输入参数应该是单引号,如果它是String.