Search code examples
iosdatabasesqlitereact-nativensfilemanager

React-native-sqlite-storage how to open database in documents directory for iOS


I am using react-native-sqlite-storage and I can not find a way to get it to open a database that is already in documents directory (downloaded from internet). The file name specified seems to point to a file in the app bundle, which when running on iOS is copied by the library from the bundle to the app's Library folder (since you cant modify files in the iOS app bundle). How can I force it to make use of a file already in the documents folder (or library folder if i move it there)?


Solution

  • React-Native-Sqlite-Storage uses a subdirectory within the apps library folder. The directory is called localDatabase. Copy, or download, or move your database to that location (localDatabase), and then open the database using React-Native-Sqlite-Storage's openDatabase() function while giving it the database name.

    var RNFS = require('react-native-fs');
    var SQLite = require('react-native-sqlite-storage');
    
    
    /*copy file from app bundle to library/localDatabase*/
    var sourcePath = RNFS.MainBundlePath + '/' + 'myDatabase.sqlite';
    var destinPath = RNFS.RNFS.LibraryDirectoryPath + '/LocalDatabase/' + 'myDatabase.sqlite';
    
    RNFS.copyFile(sourcePath, destinPath)
    .then(() =>{
        var db = SQLite.openDatabase("myDatabase.sqlite", "1.0", "", 200000, me._openCB, me._errorCB);
    })