无法在Android sqlite中创建TEMP表

时间:2012-03-08 15:13:51

标签: java android sqlite 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);

创建TEMP TABLE查询没有错误,但它抱怨tt1在第二个查询中不存在。我是以错误的方式创建TEMP表吗?

1 个答案:

答案 0 :(得分: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");