React Native / Sqlite / Expo:preload db在模拟器上工作正常,但在设备上工作不正常

时间:2019-06-26 12:05:33

标签: sqlite react-native expo

在React Native中预加载Sqlite数据库时遇到问题:在模拟器中使用以下代码可以正常工作,但在设备(iOS和Android)上均不能。你能帮我吗?

这是我App.js的一部分:

componentDidMount = () => {
    this.downloadDatabase();
  }

  downloadDatabase = async () => {
    const sqliteDirectory = `${FileSystem.documentDirectory}SQLite`;
    const { exists, isDirectory } = await FileSystem.getInfoAsync(
      sqliteDirectory
    );
    console.log(exists?"The path exists":"The path does not exists");
    if (!exists) {
      await FileSystem.makeDirectoryAsync(sqliteDirectory);
      console.log("Path created");
    } else if (!isDirectory) {
      throw new Error('SQLite dir is not a directory');
    }
    const pathToDownloadTo = `${sqliteDirectory}/Words.db`;
    const uriToDownload = Asset.fromModule(require('./assets/data/lod/Words.db')).uri;

    await FileSystem.downloadAsync(uriToDownload, pathToDownloadTo);
    console.log("DB downloaded");
    this.setState({ dbDownload: true });
  };

...

if (!this.state.isLoadingComplete && !this.props.skipLoadingScreen && !this.state.dbDownload) {
      return (
        <AppLoading
          startAsync={this._loadResourcesAsync}
          onError={this._handleLoadingError}
          onFinish={this._handleFinishLoading}
        />
      );
    } else {
      return (
        something else
      );
    }
}

还有我的其他js文件:

      const db = SQLite.openDatabase('Words.db');
      const sql = 'SELECT * FROM Words'
      db.transaction(tx => {
        tx.executeSql(
          sql,
          [],
          (tx, results) => {
            if(results.rows.length > 0) 
              this.setState({ results: results.rows._array });
            else
              this.setState({ results: [] });
          }
        );
      });

同样,在SDK上运行的模拟器上运行良好。 非常感谢您的帮助!

0 个答案:

没有答案