首页 > 解决方案 > 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!

标签: javascriptsqlitereact-nativeexpo

解决方案


我能够通过使用 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。


推荐阅读