光标的奇怪问题 - Android

发布于 2024-11-19 07:01:41 字数 931 浏览 3 评论 0原文

这是我的 DataBase 类中的一个方法:

public Cursor fetchFavTitles() {
    return myDataBase.rawQuery("SELECT rowid as _id, title
        FROM table1 JOIN table2 JOIN table3 JOIN table4 JOIN table5 JOIN table6
        WHERE fav = TRUE", null);
}

我的 SQLite 数据库有 6 个表:

  1. table1 => rowid、标题、内容、收藏夹
  2. 表2 => rowid、标题、内容、收藏夹
  3. 表3 => rowid、标题、内容、收藏夹
  4. 表4 => rowid、标题、内容、收藏夹
  5. 表5 => rowid、标题、内容、收藏夹
  6. 表6 => rowid、title、fav

在我的活动中,我这样写:

Cursor cursor = myDbHelper.fetchFavTitles();

并且应用程序强制关闭!

知道我错在哪里吗?

更新

这是LogCat的快照,我无法理解它,我用android.database过滤了输出: 在此处输入图像描述

我想要做的是获取 title (类型:TEXT)具有值 TRUE 的收藏夹(类型:BOOL) 来自所有表并将它们显示在一个 ListView 中(使用 SimpleCursorAdapter)。

This is a method In my DataBase Class:

public Cursor fetchFavTitles() {
    return myDataBase.rawQuery("SELECT rowid as _id, title
        FROM table1 JOIN table2 JOIN table3 JOIN table4 JOIN table5 JOIN table6
        WHERE fav = TRUE", null);
}

My SQLite database has 6 tables:

  1. table1 => rowid, title, content, fav
  2. table2 => rowid, title, content, fav
  3. table3 => rowid, title, content, fav
  4. table4 => rowid, title, content, fav
  5. table5 => rowid, title, content, fav
  6. table6 => rowid, title, fav

In my activity, I wrote this:

Cursor cursor = myDbHelper.fetchFavTitles();

and the application forces the close!

Any idea where I'm mistaken ?

UPDATE

This is a snapshot of the LogCat, I couldn't understand it, I filtered the output with android.database:
enter image description here

What I am trying to do is getting the title (type: TEXT) that have a fav (type: BOOL) with value TRUE From all the tables and display them in one ListView (using SimpleCursorAdapter).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幸福丶如此 2024-11-26 07:01:41

在不确切了解您要做什么的情况下,我猜您需要将查询更改为:

SELECT rowid as _id, title
        FROM table1 
        WHERE fav = TRUE
UNION ALL
SELECT rowid as _id, title
        FROM table2
        WHERE fav = TRUE
UNION ALL
SELECT rowid as _id, title
        FROM table3
        WHERE fav = TRUE
UNION ALL   
SELECT rowid as _id, title
        FROM table4 
        WHERE fav = TRUE
UNION ALL       
SELECT rowid as _id, title
        FROM table5 
        WHERE fav = TRUE
UNION ALL       
SELECT rowid as _id, title
        FROM table6 
        WHERE fav = TRUE

这将从每个表中获取“fav = TRUE”的所有结果,并将它们全部放入一个结果集中。如果您不想重复,可以将“UNION ALL”更改为“UNION”。现在您的查询失败,因为“SELECT rowid as _id, title”不知道从哪个表中提取“title”字段。

Without understanding exactly what you're going for, I'm guessing you need to change your query to:

SELECT rowid as _id, title
        FROM table1 
        WHERE fav = TRUE
UNION ALL
SELECT rowid as _id, title
        FROM table2
        WHERE fav = TRUE
UNION ALL
SELECT rowid as _id, title
        FROM table3
        WHERE fav = TRUE
UNION ALL   
SELECT rowid as _id, title
        FROM table4 
        WHERE fav = TRUE
UNION ALL       
SELECT rowid as _id, title
        FROM table5 
        WHERE fav = TRUE
UNION ALL       
SELECT rowid as _id, title
        FROM table6 
        WHERE fav = TRUE

This will take all the results where 'fav = TRUE' from each of the tables and put them all into one result set. If you don't want duplicates, you can change 'UNION ALL' to 'UNION'. Right now your query is failing because 'SELECT rowid as _id, title' doesn't know which of your tables to pull the 'title' field from.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文