Android,SQLite:附加数据库中没有此类表异常

时间:2011-12-21 13:38:54

标签: android database sqlite sqlcipher

尝试将数据复制到使用SQLCipher从普通数据库(db)加密的附加数据库(source)时,我收到“无此表”异常。

    StringBuilder attachDatabase = new StringBuilder();
    attachDatabase.append("ATTACH DATABASE '").append(this.db.getPath()).
                    append("' as ").append(NEW_DB_ALIAS).
                    append(" KEY '").append("123").append("';");
    source.execSQL(attachDatabase.toString());

    StringBuilder copyTable = new StringBuilder();
    String table = "t1";
    copyTable.append("INSERT INTO ").append(NEW_DB_ALIAS).append(".").append(table).
                append(" SELECT * FROM ").append(table).append(";");
    db.execSQL(copyTable.toString());

创建加密数据库,其方案与未加密数据库相同。可能是什么问题?

1 个答案:

答案 0 :(得分:0)

您可以验证您使用的是什么版本的SQLCipher for Android吗?我们最近发布了1.0个您可以访问的库:https://github.com/downloads/guardianproject/android-database-sqlcipher/SQLCipherForAndroid-SDK-0.0.6-FINAL.zip

我刚刚执行了以下场景,其中新数据库文件在执行之前不存在,并且运行良好。您是否可以尝试这样做而无需先使用模式创建新数据库:

    String newKey = "foo";
    File newDatabasePath = getDatabasePath("new.db");
    String attachCommand = "ATTACH DATABASE ? as encrypted KEY ?";
    String createCommand = "create table encrypted.t1(a,b)";
    String insertCommand = "insert into encrypted.t1 SELECT * from t1";
    String detachCommand = "DETACH DATABASE encrypted";
    encryptedDatabase.execSQL(attachCommand, new Object[]{newDatabasePath.getAbsolutePath(), newKey});
    encryptedDatabase.execSQL(createCommand);
    encryptedDatabase.execSQL(insertCommand);
    encryptedDatabase.execSQL(detachCommand);