【问题标题】:How to search in SQLite Database by using String variable not Integer?如何使用字符串变量而不是整数在 SQLite 数据库中搜索?
【发布时间】:2012-01-07 03:47:24
【问题描述】:

我的应用程序的一部分是从动态数据库中获取用户帐户信息。当用户输入他的用户名和密码时,我将在数据库中搜索用户名并带上与他的用户名相关的所有数据。我正在使用 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;
}  

我在查询中遇到错误。我搜索了这个问题,他们说“主键应该是整数“_id”。无论如何使用字符串而不是整数在数据库中搜索?

【问题讨论】:

    标签: android sqlite


    【解决方案1】:

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

    【讨论】:

    • 我的输入是用户输入的(用户名),我想将它与 TW_USERNAME 列进行比较以带来正确的原始数据。我不明白要做什么!
    • 我的意思是在查询 sqldatabase 时,您的用户名(用户输入的内容)应该用单引号括起来。试试吧,它会根据你的需要工作。这是SO链接,似乎有人有类似的问题。 stackoverflow.com/questions/6564215/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-18
    • 1970-01-01
    • 2021-03-17
    • 1970-01-01
    • 2021-02-15
    • 2021-02-21
    • 2023-03-13
    相关资源
    最近更新 更多