【问题标题】:SQLite for Android custom table view (SQL VIEW, not Android view) discrepancy?SQLite for Android 自定义表视图(SQL VIEW,不是 Android 视图)有差异?
【发布时间】:2010-07-16 22:10:28
【问题描述】:

我在 SQLite 数据库中为 Android 应用程序创建了一个自定义视图。
我在 Ubuntu 上使用 Sqliteman 来测试我的 SQL 语句,然后再将它们放入我的应用程序中。
我正在尝试对我的视图做一个简单的选择语句。
select 语句在 SQLiteman 中运行良好,但是当我在代码中放入相同的语句时,它会引发错误。

声明:

select * from item_view where parent_item_id = 0;

视图(转换为 Java 字符串):

"create view if not exists item_view as select " +
    "item._id, item.status, item.name, item.position, " +
    "item.parent_item_id, item.note_id, item.other_id, " + 
    "note.contents, other.priority " +
"from item, note, other where item.note_id = note._id and item.other_id = other._id"

错误:

07-16 14:21:15.153: ERROR/AndroidRuntime(5054): Caused by: android.database.sqlite.SQLiteException: no such column: parent_item_id: , while compiling: SELECT * FROM item_view WHERE parent_item_id = 0

我首先尝试在我的 select 语句中调用字段 item.parent_item_id,但没有成功。
然后我拉出数据库并用 Sqliteman 打开它。
这些字段按照它们在原始表中的状态列出(_id、状态、名称等)
所以我在 Sqliteman 中运行了上面的 SQL,并且能够毫无问题地检索到适当的数据,但我无法让它在我的应用程序中工作。
我还注意到,将视图作为 DROP TABLE 命令删除在 SQLiteman 中有效,但在我的应用程序中无效。
我想知道我是否可能缺少其他一些特定于 VIEW 的功能。
不过,我在 android 文档或任何 SQL 文档中都没有看到任何内容。

从技术上讲,我可以使用原始表通过更复杂的 SQL 调用来做到这一点,但我的所有表都遵循某些准则,以便我可以动态生成 SQL 调用。我想让视图表工作以保持我的代码统一并始终重复使用相同的调用以避免重复代码引起的维护和其他错误问题。

【问题讨论】:

    标签: sql android sqlite


    【解决方案1】:

    事实证明,您需要专门命名您正在创建的每个字段,以便 android 可以将其视为常规表。 解决办法...

    "create view if not exists item_view as select " +
        "item._id as _id, item.status as item_status, item.name as item_name, item.position as item_position, " +
        "item.parent_item_id as item_parent_item_id, item.note_id as item_note_id, item.other_id as item_other_id, " + 
        "note.contents as note_contents, other.priority as other_priority " +
    "from item, note, other where item.note_id = note._id and item.other_id = other._id"
    

    【讨论】:

    • 我无法强调您对我的帮助有多大!拔了两个小时的头发,试图找出为什么该死的 android 找不到 absolutely 存在的列!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多