【问题标题】:React-native-sqlite-storage how to open database in documents directory for iOSReact-native-sqlite-storage 如何在 iOS 的文档目录中打开数据库
【发布时间】:2017-12-14 22:48:39
【问题描述】:
我正在使用react-native-sqlite-storage,但我找不到方法让它打开已经在文档目录中的数据库(从互联网下载)。指定的文件名似乎指向应用程序包中的一个文件,当在 iOS 上运行时,该文件由库从包复制到应用程序的库文件夹(因为您无法修改 iOS 应用程序包中的文件)。如何强制它使用文档文件夹(或库文件夹,如果我将它移到那里)中的文件?
【问题讨论】:
标签:
ios
database
sqlite
react-native
nsfilemanager
【解决方案1】:
React-Native-Sqlite-Storage 使用应用程序库文件夹中的子目录。该目录称为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);
})