【问题标题】:SQLiteException when querying a SELECT [closed]查询 SELECT 时出现 SQLiteException [关闭]
【发布时间】:2018-07-01 05:01:27
【问题描述】:

我正在开发一个带有 SQLite 数据库的 Android 应用程序。它记录交易。 transactions 表有以下列:

  • idTransaction INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT
  • date DATE NOT NULL
  • type INT NOT NULL
  • details VARCHAR(100) NOT NULL
  • notes VARCHAR(300) NOT NULL
  • amount DOUBLE NOT NULL
  • account INT NOT NULL
  • category INT NOT NULL

我还做了一个方法来获取所有事务的游标:

// Select all Transactions
protected Cursor getTransactions() throws SQLException {
    Cursor mCursor = null;
    String[] colums = new String[]{"idTransaction", "date", "type", "details", "notes", "amount", "account", "category"};
    mCursor = this.getReadableDatabase().query("Transactions", colums, null, null, null, null, "name ASC");
    return mCursor;
}

在其他类中,我创建了一个方法来移动光标,当我调用 getTransactions() 方法时。

但异常发生在这一行:

mCursor = this.getReadableDatabase().query("Transactions", colums, null, null, null, null, "name ASC");

android.database.sqlite.SQLiteException: no such column: name(Sqlite code 1): , while compile: SELECT idTransaction, date, type, details, notes, amount, account, category FROM Transactions ORDER BY name ASC,(操作系统错误 - 2:没有这样的文件或目录)

我检查了表名和所有列名。我删除并重新安装了该应用程序,但我不知道为什么会发生异常。

【问题讨论】:

  • 您列出了 8 列,其中没有一个称为 name,并且您要求按 name 升序排序(这就是 "name ASC" 的意思),那么您为什么会因为它失败而感到困惑“没有这样的专栏:name"?错误信息说明了一切。

标签: java android sqlite exception


【解决方案1】:

您正在使用name ASC 的订单,但您没有name 列。这就是错误消息中显示no such column: name时的含义

【讨论】:

  • 如果这真的是解释,那么你可能应该投票结束作为一个错字问题。
  • 我不敢相信!但有时会发生。 :-P
【解决方案2】:

您使用的查询方法的格式为

query(String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy)

如文档所述Here

正如您提到 orderBy"name ASC",但不幸的是,您的查询字符串不包含任何此类列,因此导致您提到的异常.

要解决此问题,请在列列表中也添加 name 列,否则将 orderBy 更改为任何现有列名 例如-“日期 ASC”强>。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-11
    • 1970-01-01
    • 1970-01-01
    • 2014-08-27
    • 2020-08-01
    • 2014-11-18
    相关资源
    最近更新 更多