Android 查询已创建的dbView(非表)时出错!

Android 查询已创建的dbView(非表)时出错!,android,database,sqlite,view,Android,Database,Sqlite,View,有关上下文,请参见 根据yock提供的一些建议,我改变了数据库设计,使用外键。您可以在本文档中检查整个数据库设计 所以我有两张桌子 TABLE(1) TABLE (2) +--------------------------------------+ +-----------+ | SURVEYS TABLE | | ICONS | +----+---

有关上下文,请参见

根据yock提供的一些建议,我改变了数据库设计,使用外键。您可以在本文档中检查整个数据库设计

所以我有两张桌子

              TABLE(1)                         TABLE (2)
+--------------------------------------+     +-----------+
|            SURVEYS TABLE             |     |   ICONS   |
+----+------+-------------+------------+     +----+------+
| ID | name | description |   iconID   |     | ID | ICON |
+----+------+-------------+------------+     +----+------+
|    |      |             | FOREIGN KEY|     
+--------------------------------------+     
我使用这两个表构建了一个视图

public static final String VIEW_SURVEYS = "surveysview";

      public static final String VIEW_SURVEYS_CREATE =
       "CREATE VIEW " + VIEW_SURVEYS +
       " AS SELECT " + TABLE_SURVEYS + "." + KEY_ROWID + " AS _id, " + 
       TABLE_SURVEYS + "." + KEY_NAME + ", " +
       TABLE_SURVEYS + "." + KEY_DESCRIPTION + ", " +
       TABLE_ICONS + "." + KEY_ICON + " " +
       "FROM " + TABLE_SURVEYS + " JOIN " + TABLE_ICONS +
       " ON " + TABLE_SURVEYS + "." + KEY_ICONID + " = " +
                TABLE_ICONS + "." + KEY_ROWID;
这些表和视图是在我的适配器构造函数中调用的

private static class DatabaseHelper extends SQLiteOpenHelper 
{
    DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

    // TABLES
    db.execSQL(TABLE_ICONS_CREATE);
    db.execSQL(TABLE_SURVEYS_CREATE);

    // VIEWS
    db.execSQL(VIEW_SURVEYS_CREATE);
    }
这是用于查询视图的方法

    public Cursor getSurveys() {
return db.query(VIEW_SURVEYS, new String[] {
        KEY_ROWID, KEY_NAME, KEY_DESCRIPTION, KEY_ICON},
        null, null, null, null, null);
}
在我的活动中通过游标调用

    DBAdapter db = new DBAdapter(this);
    db.open(); // Opens db session 
    Cursor survey_cursor = db.getSurveys();
    startManagingCursor(survey_cursor);
问题是当我尝试运行上面的代码时,我的程序崩溃了。它所在的行是Cursor survey_Cursor=db.getSurveys()

和Catlog秀

11-25 16:19:42.324: ERROR/AndroidRuntime(6146): Caused by: android.database.sqlite.SQLiteException: no such table: surveysview: , while compiling: SELECT _id, name, description, icon FROM surveysview
我反复阅读了无数次代码,没有发现任何错误。我错过了什么

以下是sqlite_master的SELECT*输出

table|android_metadata|android_metadata|3|CREATE TABLE android_metadata (locale TEXT)

table|fieldtype|fieldtype|4|CREATE TABLE fieldtype (_id integer primary key autoincrement, type text not null)

table|sqlite_sequence|sqlite_sequence|5|CREATE TABLE sqlite_sequence(name,seq)

table|editicons|editicons|6|CREATE TABLE editicons (_id integer primary key autoincrement, icon text not null)

table|surveys|surveys|7|CREATE TABLE surveys (_id integer primary key autoincrement, name text not null, description text, editableid integer not null, FOREIGN KEY(editableid) REFERENCES editicons(_id))

table|questions|questions|8|CREATE TABLE questions (_id integer primary key autoincrement, field text not null, typeid text not null, survey_id integer not null, FOREIGN KEY(typeid) REFERENCES fieldtype(_id)FOREIGN KEY(survey_id) REFERENCES surveys(_id))

table|choices|choices|9|CREATE TABLE choices (choice text, question_id integer not null, FOREIGN KEY(question_id) REFERENCES questions(_id))

table|answers|answers|10|CREATE TABLE answers (choice_id integer not null, FOREIGN KEY(choice_id) REFERENCES choices(_id))

view|surveysview|surveysview|0|CREATE VIEW surveysview AS SELECT surveys._id AS _id, surveys.name AS name, surveys.description AS description, editicons.icon AS icon FROM surveys JOIN editicons ON surveys.editableid = editicons._id

(此输出反映了我在每列后面添加“AS”后对代码所做的一些更改)

也许android.database.sqlite.SQLiteDatabase不支持调用
查询时的视图
;尝试使用
rawQuery
,例如:

return db.rawQuery("select * from " + VIEW_SURVEYS);
如果可行,请用您的列名替换
*

下面是我使用您的模式在sqlite3 shell中得到的内容:

e$ sqlite3
SQLite version 3.7.3
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> CREATE TABLE editicons (_id integer primary key autoincrement, icon text not null);
sqlite> CREATE TABLE surveys (_id integer primary key autoincrement, name text not null, description text, editableid integer not null, FOREIGN KEY(editableid) REFERENCES editicons(_id));
sqlite> CREATE VIEW surveysview AS SELECT surveys._id AS _id, surveys.name AS name, surveys.description AS description, editicons.icon AS icon FROM surveys JOIN editicons ON surveys.editableid = editicons._id;
sqlite> select * from surveysview;
sqlite> insert into editicons values (null,"icon1");
sqlite> select * from editicons;
1|icon1
sqlite> insert into surveys values (NULL, 'survey1', 'survey1 desc', 1);
sqlite> select * from surveysview;
1|survey1|survey1 desc|icon1
sqlite> 

因此,如果某个东西是amis,那么它必须在Android实现中。

可能Android.database.sqlite.SQLiteDatabase在调用
查询时不支持视图;尝试使用
rawQuery
,例如:

return db.rawQuery("select * from " + VIEW_SURVEYS);
如果可行,请用您的列名替换
*

下面是我使用您的模式在sqlite3 shell中得到的内容:

e$ sqlite3
SQLite version 3.7.3
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> CREATE TABLE editicons (_id integer primary key autoincrement, icon text not null);
sqlite> CREATE TABLE surveys (_id integer primary key autoincrement, name text not null, description text, editableid integer not null, FOREIGN KEY(editableid) REFERENCES editicons(_id));
sqlite> CREATE VIEW surveysview AS SELECT surveys._id AS _id, surveys.name AS name, surveys.description AS description, editicons.icon AS icon FROM surveys JOIN editicons ON surveys.editableid = editicons._id;
sqlite> select * from surveysview;
sqlite> insert into editicons values (null,"icon1");
sqlite> select * from editicons;
1|icon1
sqlite> insert into surveys values (NULL, 'survey1', 'survey1 desc', 1);
sqlite> select * from surveysview;
1|survey1|survey1 desc|icon1
sqlite> 
因此,如果某个东西是amis,那么它一定在Android实现中。

啊,终于找到了

填充图标表的方法中存在错误。奇怪的是,没有抛出错误。因此,当视图访问该表时,错误会传播

事实上,我在读取ICONS表的内容时,通过adb控制台发现了这个bug

纠正了这一点,现在就像一个符咒。

啊,终于找到了

填充图标表的方法中存在错误。奇怪的是,没有抛出错误。因此,当视图访问该表时,错误会传播

事实上,我在读取ICONS表的内容时,通过adb控制台发现了这个bug


更正了这一点,现在它就像一个符咒一样工作。

返回db.rawQuery(“select*from”+VIEW\u SURVEYS,null);出现相同错误。是否数据库已经存在,并且没有调用onCreate方法?您可以使用onOpen并将“IF NOT EXISTS”添加到表和视图创建语句中。我已经在emulator上擦除了所有用户数据(我通常在处理数据库和初始化参数时这样做)。真正让人头疼的是,除了查询“视图”之外,所有查询都可以工作。我开始怀疑Android SQlite是否支持DB视图(就像它本身不支持外键约束一样!),这会返回什么?从sqlite_master中选择*,其中type='view';查询:从sqlite_master中选择*;将告诉您(和我们!)创建了哪些表和视图以及它们的列名。返回db.rawQuery(“select*from”+VIEW\u SURVEYS,null);出现相同错误。是否数据库已经存在,并且没有调用onCreate方法?您可以使用onOpen并将“IF NOT EXISTS”添加到表和视图创建语句中。我已经在emulator上擦除了所有用户数据(我通常在处理数据库和初始化参数时这样做)。真正让人头疼的是,除了查询“视图”之外,所有查询都可以工作。我开始怀疑Android SQlite是否支持DB视图(就像它本身不支持外键约束一样!),这会返回什么?从sqlite_master中选择*,其中type='view';查询:从sqlite_master中选择*;将告诉您(和我们!)创建了哪些表和视图以及它们的列名。