简体   繁体   English

无法在Android sqlite中创建TEMP表

[英]Cannot create TEMP table in Android sqlite

I have tried to create a temporary table (sqlite) in Android 我试图在Android中创建一个临时表(sqlite)

Here is the code snippet: 这是代码片段:

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

There is no error for the create TEMP TABLE query, but it complains tt1 is not existed in the second query. 创建TEMP TABLE查询没有错误,但它抱怨tt1在第二个查询中不存在。 Am I create TEMP table in a wrong way? 我是以错误的方式创建TEMP表吗?

Typically you shouldn't be using rawQuery for creating tables and doing inserts - try using SQLiteDatabase#execSQL . 通常,您不应该使用rawQuery来创建表和执行插入 - 尝试使用SQLiteDatabase#execSQL

This example works at least: 此示例至少起作用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM