我需要使用react-native-sqlite-storage (由dryganet:https://github.com/dryganets/react-native-sqlite-storage/tree/sergeyd/sqlite-cipher修改)打开预先填充的加密数据库,这个预先填充的数据库是使用react-native-fs从远程位置下载的。
此时,当我将数据库放入assets文件夹(这个数据库是在react-native run-android命令期间复制的)并触发方法“openDatabase”时:
const dbName = "myDatabase.db";
const dbLocation = "~database.db";
const encryptionLey = "asdfghasdfgh";
const queryDatabase = async (tx) => {
const [sqliteTx, results] = await tx.executeSql(sqlQuery);
resultData = results;
};
const db = await SQLite.openDatabase({
name: dbName,
createFromLocation: dbLocation,
key: encryptionKey
}, (result) => {...}, (result) => {...});
await db.transaction(queryDatabase);
retrun resultData;一切正常,但是我需要在运行时下载这个数据库,所以...当我将dbLocation更改为:
const dbLocation = fs.DocumentDirectoryPath + "/database.db";并从远程位置下载sqlite数据库:
fs.downloadFile({,
fromUrl: "http://10.0.2.2:63074/Database/GetDatabase",
toFile: dbLocation,
}).promise.then(res => {
fs.exists(dbLocation).then(fileExist => {
...
});
});所有东西都停止工作,没有错误发生,在调试控制台中我可以看到:
OPEN database: myDatabase.db
new transaction is waiting for open operation问题出在哪里?我使用SQLCipher对数据库进行加密和解密。也许还有另一种方法可以实现这一点,这个解决方案必须同时适用于安卓和iOS?
发布于 2017-09-19 23:59:18
好吧,我自己找到了解决方案,在原生Java代码中,有这样的地方:
if (assetFilePath != null && assetFilePath.length() > 0) {
if (assetFilePath.compareTo("1") == 0) {
assetFilePath = "www/" + dbname;
in = this.getContext().getAssets().open(assetFilePath);
FLog.v(TAG, "Located pre-populated DB asset in app bundle www subdirectory: " + assetFilePath);
} else if (assetFilePath.charAt(0) == '~') {
assetFilePath = assetFilePath.startsWith("~/") ? assetFilePath.substring(2) : assetFilePath.substring(1);
in = this.getContext().getAssets().open(assetFilePath);
FLog.v(TAG, "Located pre-populated DB asset in app bundle subdirectory: " + assetFilePath);
} else {
File filesDir = this.getContext().getFilesDir();
assetFilePath = assetFilePath.startsWith("/") ? assetFilePath.substring(1) : assetFilePath;
File assetFile = new File(filesDir, assetFilePath);
in = new FileInputStream(assetFile);
FLog.v(TAG, "Located pre-populated DB asset in Files subdirectory: " + assetFile.getCanonicalPath());
if (openFlags == SQLiteDatabase.OPEN_READONLY) {
dbfile = assetFile;
FLog.v(TAG, "Detected read-only mode request for external asset.");
}
}
}这意味着在这种情况下我需要设置createFromLocation:"database.db",因为路径的其余部分将在java本机代码中自动添加。
https://stackoverflow.com/questions/46299268
复制相似问题