mob*_*ner 7 java sqlite android temp-tables
我试图在Android中创建一个临时表(sqlite)
这是代码片段:
// No error - But cannot create TEMP table
database.rawQuery("CREATE TEMP TABLE IF NOT EXISTS tt1 (unread_message int, target varchar)", null);
// Error - android.database.sqlite.SQLiteException: no such table: tt1: , while compiling: INSERT INTO tt1 SELECT count(*), target FROM messages where read_status=0 and direction=1 GROUP BY target
database.rawQuery("INSERT INTO tt1 SELECT count(*), target FROM messages where read_status=0 and direction=1 GROUP BY target", null);
Run Code Online (Sandbox Code Playgroud)
创建TEMP TABLE查询没有错误,但它抱怨tt1在第二个查询中不存在.我是以错误的方式创建TEMP表吗?
Jen*_*ens 12
通常,您不应该rawQuery用于创建表和插入 - 尝试使用SQLiteDatabase#execSQL.
此示例至少起作用:
SQLiteOpenHelper dummy = new SQLiteOpenHelper(this, "mobileAppBeginner.db", null, 1) {
@Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}
@Override public void onCreate(SQLiteDatabase db) {}
};
SQLiteDatabase db = dummy.getWritableDatabase();
db.execSQL("CREATE TEMP TABLE messages (read_status INTEGER, direction INTEGER, target TEXT)");
db.execSQL("CREATE TEMP TABLE IF NOT EXISTS tt1 (unread_message int, target varchar)");
db.execSQL("INSERT INTO tt1 SELECT count(*), target FROM messages where read_status=0 and direction=1 GROUP BY target");
Run Code Online (Sandbox Code Playgroud)