react-native-nitro-sqlite icon indicating copy to clipboard operation
react-native-nitro-sqlite copied to clipboard

changing location property is not showing the DB

Open devaarx opened this issue 2 years ago • 3 comments

I am trying to save the DB file inside the App Data folder (e.g. /storage/emulated/0/Android/data/com.package.name). And I have this code:

// other imports
import { QuickSQLiteConnection, open } from "react-native-quick-sqlite";

// get the app directory
const externalDirectory = RNFS.ExternalDirectoryPath; // --> /storage/emulated/0/Android/data/com.package.name/files location

// open the db
const db = open({ name: "default.db", location: externalDirectory });

This opens the DB fine, I am not able to see any file inside the above mentioned location. And, if I change the location property to any other directory (e.g. RNFS.DownloadDirectoryPath), this also not showing any DB, but if I revert to old location, I still have the older data.

My requirement is that I want to give an option to the user to export (backup) the local database, so that they can import it later, or onto any other devices. How can I achieve this?

Thank you in advance!

devaarx avatar Sep 17 '23 13:09 devaarx

I have the same problem: after modifying the location, I can't see the database file. Have you solved this problem?

xiaoxiaoboa avatar Oct 15 '23 05:10 xiaoxiaoboa

I have the same problem: after modifying the location, I can't see the database file. Have you solved this problem?

For now I am doing a "hack" like this:

SQLite.openDatabase({ name: DB_NAME, location: "default" });

const dbFileLocation = RNFS.DocumentDirectoryPath.split(PACKAGE_NAME)[0] + PACKAGE_NAME;
const dbFilePath = dbFileLocation + "/databases/" + DB_NAME;

The RNFS.DocumentDirectoryPath will give the app data location + /files. So I split with my package name, and appending /databases/ + DB_NAME, so you will end up with your database file. You can copy it or do whatever you'd like. I am not sure if this will work in iOS.

devaarx avatar Oct 19 '23 18:10 devaarx

location only appends to the default path (NSDocumentsDirectory on iOS and the files dir on android).

It doesn't absolutely replace the path. If you want to navigate to the databases folder you can use a relative path:

SQLite.openDatabase({name: DB_NAME, location: '../databases'})

The problem is that doing it this way might break in future Android versions where the database path might change.

ospfranco avatar Nov 09 '23 13:11 ospfranco