javascript - expo sqlite use existing database
问题描述
According to the expo sqlite documentation for react-native I can initialize a db like so:
const db = SQLite.openDatabase('db.db');
This works and I can update the db like so:
update() {
db.transaction(tx => {
tx.executeSql(
`select * from items where done = ?;`,
[this.props.done ? 1 : 0],
(_, { rows: { _array } }) => this.setState({ items: _array })
);
});
}
From my limited understanding this creates a database in the device. And then it's manipulated keeping all the db local.
I have a database with all the necessary tables already setup. How can I have it use the current database I already have setup?
For example: (not correct syntax)
const db = SQLite.openDatabaseIShipWithApp('mypath/mydb.db');
I couldn't find any documentation to help me with this. The only reason I mention the above is because I already have the db with the tables and data.
Any help would be appreciated!
解决方案
我能够通过使用 expo 的 FileSystem.downloadAsync 来实现这一点:
首先我导入它,因为我使用的是 expo 托管应用程序:
import { FileSystem } from 'expo';
然后我像这样从服务器下载它:
// load DB for expo
FileSystem.downloadAsync(
'http://example.com/downloads/data.sqlite',
FileSystem.documentDirectory + 'data.sqlite'
)
.then(({ uri }) => {
console.log('Finished downloading to ', uri)
})
.catch(error => {
console.error(error);
})
第一个参数是位置的 uri,第二个参数是我想要放置它的位置。这里我使用的是 documentDirectory。
推荐阅读
- docker - 向外部用户打开 docker daemon 命令错误
- mongodb - 向模式添加方法时打字稿和 MongoDB 类型擦除
- linker - COMDAT 部分没有符号
- reactjs - 根据 Props.Children 中的值进行条件样式
- javascript - 无法从使用外来对象在画布中绘制的 svg 中提取“base 64”
- groovy - 如何在 Nifi 中比较两个 CSV 文件?
- sql - 检索由未响应 to_sql 的 active_record 方法执行的原始 SQL
- java - jersey 2.22 中的 servicelocator impl 关闭问题
- nginx - 位置将所有 /*.xml 重定向到 /*
- javascript - 反应应用程序中的套接字io多个连接