1

i need to open prepopulated, encrypted database using react-native-sqlite-storage (modified by dryganet: https://github.com/dryganets/react-native-sqlite-storage/tree/sergeyd/sqlite-cipher), this prepopulated datatabase is downloaded from remote location using react-native-fs.

At this moment when i put database in assets folder (this database is copied during react-native run-android command) and fire method "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;

everything works correctly, but i need to download this database during runtime, so... when i change dbLocation to:

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

and download sqlite database from remote location:

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

Everything stops working, no errors occures, in debug console i can see this:

OPEN database: myDatabase.db
new transaction is waiting for open operation

Where is the problem? I uses SQLCipher to encrypt and decrypt database. Maybe there is another way to achieve this, the solution must works both on Android and iOS?

1
  • did you find a solution in ReactNative? Commented Sep 27, 2019 at 14:15

1 Answer 1

2

Ok, i found solution by myself, in Native Java code, there is place like:

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

That means i need to set createFromLocation:"database.db" in this case, because rest of the path will be added automaticaly in java native code.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.