React-native-sqlite-storage如何在iOS的文档目录中打开数据库

时间:2017-07-11 03:40:05

标签: ios database sqlite react-native nsfilemanager

我正在使用react-native-sqlite-storage,我找不到办法让它打开已经在文件目录中的数据库(从互联网上下载)。指定的文件名似乎指向应用程序包中的文件,该文件在iOS上运行时由库从包复制到应用程序库文件夹(因为您无法修改iOS应用程序包中的文件)。如何强制它使用文档文件夹中的文件(如果我在那里移动文件夹,则使用库文件夹)?

1 个答案:

答案 0 :(得分:0)

React-Native-Sqlite-Storage使用apps库文件夹中的子目录。该目录名为localDatabase。复制,下载或将数据库移动到该位置(localDatabase),并使用React-Native-Sqlite-Storage的openDatabase()函数打开数据库,同时为其提供数据库名称。

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