光标的奇怪问题 - Android

时间:2011-07-11 07:07:12

标签: android sqlite android-cursor

这是我的数据库类中的一个方法:

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,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
  7. 在我的活动中,我写了这个:

    Cursor cursor = myDbHelper.fetchFavTitles();
    

    并且应用程序强行关闭!

    知道我错了吗?

    更新

    这是LogCat的快照,我无法理解,我用android.database过滤了输出: enter image description here

    我想要做的是获得具有值为TRUE的title(类型:TEXT),其值为TRUE从所有表中显示并显示在一个ListView中(使用SimpleCursorAdapter)。

1 个答案:

答案 0 :(得分:1)

如果不准确了解您的目标,我猜您需要将查询更改为:

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'字段。