在React-Native应用程序上下载并使用SQLite数据库

时间:2017-09-19 11:28:58

标签: database sqlite encryption react-native

我需要使用package javatestapp; import java.awt.Color; import java.util.Timer; import java.util.TimerTask; import javax.swing.JFrame; public class JavaTestApp extends TimerTask { static int x = 100; static int y = 0; static int z = 0; static int Max = 255; static int height = 500; static int width = 500; static JFrame frame; public static void main(String[] args) { InitFrame(); } public static void incrementValues() { if (x < Max) { x ++; } else { x = 0; y ++; } if (y > Max) { y = 0; z ++; } if (z > Max) { x = 0; y = 0; z = 0; } } public static void genImage() { Color color = new Color(x,y,z,255); frame.getContentPane().setBackground(color); } public static void InitFrame() { frame = new JFrame("Bildergenerator"); frame.setSize(width,height); frame.setVisible(true); Timer timer = new Timer(true); TimerTask timerTask = new JavaTestApp(); timer.scheduleAtFixedRate(timerTask, 0, 10); } @Override public void run() { incrementValues(); genImage(); } } 打开预先填充的加密数据库(由dryganet修改:https://github.com/dryganets/react-native-sqlite-storage/tree/sergeyd/sqlite-cipher),使用react-native-sqlite-storage从远程位置下载此预填充数据库。

此时我将数据库放入assets文件夹(此数据库在react-native-fs命令期间复制)和fire方法“openDatabase”:

react-native run-android

一切正常,但我需要在运行时下载此数据库,所以...当我将dbLocation更改为:

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;

从远程位置下载sqlite数据库:

const dbLocation = fs.DocumentDirectoryPath + "/database.db";

一切都停止工作,没有错误发生,在调试控制台我可以看到:

fs.downloadFile({,
      fromUrl: "http://10.0.2.2:63074/Database/GetDatabase",
      toFile: dbLocation,
    }).promise.then(res => {
      fs.exists(dbLocation).then(fileExist => {
        ...
      });
    });

问题出在哪里?我使用SQLCipher加密和解密数据库。 也许有另一种方法可以实现这一点,解决方案必须适用于Android和iOS?

1 个答案:

答案 0 :(得分:2)

好的,我自己找到了解决方案,在Native 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本机代码中。