【问题标题】:Android-Cursor stays empty after successfull SQLite-DatabasequerySQLite-Database Query 成功后,Android-Cursor 保持为空
【发布时间】:2012-10-11 08:58:12
【问题描述】:

在网上搜索 3 天并尝试不同的东西后,我的知识已经结束了。 我正在尝试通过光标从 Android-SQLite 数据库中获取数据。数据库是用这个 SQLCode 创建的:

create table Passwort("ref text primary key, pw text)";

Buggcode 是这样的:

public String suchen(String refe) {

    String antwort = "";
    open();
    try {
        String rawQuery = "Select * FROM Passwort WHERE ref LIKE \"" + refe
                + "\"";
        // Cursor c= db.query("Passwort", null, DBspalten[0]+"= '"+refe+"'",
        // null, null, null, null);
        Cursor c = db.rawQuery(rawQuery, null);
        System.out.println(c.moveToFirst());
        // normaly the "antwort" would  be the second field of the Cursors first entry 
        // as told, the cursors  is always empty
        //antwort = "";
        System.out.println("testausgabe:" + " " + c.getCount());
    } catch (SQLException e) {
        System.out.println("anderer Fehler");
    }
    return antwort;
}

我通过成功的插入语句插入了一个带有 ref="test" pw="QqjMs740Mg" 的条目 该数据在数据库中是确定的,我通过数据库中的 Eclipse 插件找到了它。 那么我的 Cursor 的问题是什么,activ System.out.println-statment 总是返回 false,这意味着 Cursor 中没有数据。

【问题讨论】:

  • 我用于测试的版本是API-10,因为我自己的智能手机有点旧

标签: java android-sqlite android-cursor


【解决方案1】:

试试这个:

String rawQuery = "Select * FROM Passwort WHERE ref LIKE '" + refe + "'";

我只是用单引号替换了双引号。所以 sql 查询将是:

SELECT * FROM Passwort WHERE ref LIKE 'your_refe_value'

让我知道它是否有效。

【讨论】:

  • 我之前尝试过,结果相同,同时我找到了一个解决方案,但是这个有一个很大的缺点:我只是将模拟器的 API 级别从 10 更改为 14 并且它有效,所以这可能是 10 级以下实施的普遍问题,我记得我之前的 select 也有问题,那个时候 Statment 确实提供了数据,但并不是所有的都要求数据。
猜你喜欢
  • 1970-01-01
  • 2015-02-02
  • 1970-01-01
  • 2015-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-19
  • 2012-02-20
相关资源
最近更新 更多