Sqlite附加数据库并将数据从表A插入表B

时间:2017-11-12 13:44:03

标签: android database sqlite insert

我有两个sqlite数据库,想要将数据库A中的表复制到数据库B.不应复制数据库A中的其他表。 我尝试ATTACH数据库,然后使用INSERT但有异常"没有这样的表"在附表上。 我的代码:

private final String TABLE_NAME = "LOAD_IMAGES";
private final String COLUMN_UNIT_ID = "UNIT";
private final String COLUMN_LOAD = "LOAD";

public void createTable(){
    String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " ("
            + COLUMN_UNIT_ID + " INTEGER NOT NULL, "
            + COLUMN_LOAD + " INTEGER NOT NULL DEFAULT 0);";

    database.execSQL(CREATE_TABLE);
}

public void insertData() {
    String path = App.filesDir + '/' + "data";
    database.execSQL("ATTACH DATABASE '" + path + "' AS data;");
    String ADD_DATA_INTO_TABLE = "INSERT INTO '" + TABLE_NAME + "'(" 
    + COLUMN_UNIT_ID + ", " + COLUMN_LOAD +") " +"SELECT " + 
    COLUMN_UNIT_ID + ", " + COLUMN_LOAD + " FROM 'data.IMAGES';";

}

异常日志:

android.database.sqlite.SQLiteException: no such table: data.IMAGES (code 1): , while compiling: INSERT INTO 'LOAD_IMAGES'(UNIT, LOAD) SELECT UNIT, LOAD FROM 'data.IMAGES';

我试着写:

LOAD FROM data.IMAGES

LOAD FROM 'data'.'IMAGES'

LOAD FROM data'.'IMAGES

但没有改变。

第二个数据库表"图像"结构:enter image description here

1 个答案:

答案 0 :(得分:1)

FROM 'data.IMAGES'

这被解释为普通表名。删除引号。