首页 > 解决方案 > 我如何知道在 expo 应用程序中完成的 sqlite 数据库加载

问题描述

在应用程序加载后不久执行查询时 SQLite 数据库损坏

这是 Expo(react-native) 应用程序,数据库是从本地存储加载的。

如果我等待几秒钟使用应用程序(执行查询),则问题不会发生。

也许,在从本地存储加载所有数据之前执行查询时会发生此问题。

在 APP.js 中

async function loadResourcesAsync() {
  await Promise.all([
 FileSystem.makeDirectoryAsync(`${FileSystem.documentDirectory}SQLite`, {
      intermediates: true
    }).then(
      FileSystem.downloadAsync(
        Asset.fromModule(require("./assets/db/mydb.db")).uri,
        `${FileSystem.documentDirectory}SQLite/mydb.db`
      )
    )
  ]);
}

在这样的 Screen.js 中

const db = SQLite.openDatabase("mydb.db");


db.transaction(
        tx => {
          tx.executeSql(sql, [queryChar], (_, { rows: { _array } }) => {
            // console.log(JSON.stringify(_array));
            setSearchResult(_array);
          });
        },
        e => {
          console.log(e);
          });
        },
        null
      );

我期待openDatabase函数的回调,但它没有这样的回调。

我怎么知道数据库加载完成了?

标签: sqlitereact-nativeexpo

解决方案


推荐阅读