2010-11-25 7 views
1

コンテキストについてpost作成したdbView(テーブルではありません)を照会する際のエラー!

yockによって提供されたawsome sugestionに続いて、私は外部キーを使用するようにデータベース設計を変更しました。このpostではデータベース全体のデザインを確認できます。

私は2つのテーブルを持っています。

   TABLE(1)       TABLE (2) 
+--------------------------------------+  +-----------+ 
|   SURVEYS TABLE    |  | ICONS | 
+----+------+-------------+------------+  +----+------+ 
| ID | name | description | iconID |  | ID | ICON | 
+----+------+-------------+------------+  +----+------+ 
| |  |    | FOREIGN KEY|  
+--------------------------------------+  

私は2つのテーブルを使用してVIEWを構築しました。

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); 
    } 

これは、ビューのクエリに使用されるメソッドです。カーソル

DBAdapter db = new DBAdapter(this); 
    db.open(); // Opens db session 
    Cursor survey_cursor = db.getSurveys(); 
    startManagingCursor(survey_cursor); 

を通して私の活動に呼び出された

public Cursor getSurveys() { 
return db.query(VIEW_SURVEYS, new String[] { 
     KEY_ROWID, KEY_NAME, KEY_DESCRIPTION, KEY_ICON}, 
     null, null, null, null, null); 
} 

私は上記のコードを実行しようとすると、問題は、私のプログラムがクラッシュしています。それがクラッシュする行は、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
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 

SELECT * FROM用の出力(この出力は、私は、各列の後に "AS" を追加するので、私は、コードに加えられたいくつかの変更を反映)

答えて

1

おそらくandroid.database.sqliteです.SQLiteDatabaseはqueryへの呼び出しでビューをサポートしません。これが動作する場合

return db.rawQuery("select * from " + VIEW_SURVEYS); 

、お使いのカラム名と*を置き換えますようrawQuery使用してみてください。

は、ここで私はあなたのスキーマを使用してsqlite3のシェルで何を得るのです:何かがある場合

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> 

だから、それはAndroidの実装でなければなりませんAMIS。

+0

私はそれを撃つ! – Tivie

+0

リターンdb.rawQuery( "select * from" + VIEW_SURVEYS、null);同じエラーが発生しました。 – Tivie

+0

データベースがすでに存在し、onCreateメソッドが呼び出されていない可能性がありますか? onOpenを使用し、テーブルに "IF NOT EXISTS"を追加して作成ステートメントを表示することができます –

0

AH最後に見つけました!

ICONSテーブルを実装したメソッドでエラーが発生しました。不思議なことに、エラーは投げられませんでした。したがって、ビューがそのテーブルにアクセスすると、エラーが伝播しました。

ICONSテーブルの内容を読み込んだときに、実際にはこの問題がadbコンソールから検出されました。

修正されました。魅力的です。

関連する問題