【问题标题】:Unable to connect to SQLite in React-Native无法在 React-Native 中连接到 SQLite
【发布时间】:2021-12-21 04:35:18
【问题描述】:

我使用 DB Browser for SQLite 预定义了 sqlite 数据库。我已将 db 文件放在路径 root/android/app/src/main/assets/www/mysqlite.db 中,不幸的是我无法 连接。以下是我的版本控制。

Samsung Galaxy Android 11,
"react-native-sqlite-storage": "^6.0.1",
"react": "17.0.2",
"react-native": "0.65.1",
"@react-navigation/native": "^6.0.4",

我的脚本(我把它简化了):

import SQLite from 'react-native-sqlite-storage';
SQLite.DEBUG(true);
SQLite.enablePromise(false);

export const AppSignIn = (props) => {
    const OpenDB = () => {
        return new Promise((resolve, reject) => {
            global.db = SQLite.openDatabase(
                {
                    name: 'mysqlite.db',
                    createFromLocation: '~mysqlite.db',
                },
                () => {
                    console.log("Connection success!");
                },
                error => {
                    console.log(error);
                    reject();
                });
            resolve();
        });
    }
    const ReadDB = () => {
        return new Promise((resolve) => {
            global.db.transaction(function (tx) {
                tx.executeSql(
                    // The rest of the trx
                );
                resolve();
            });
        });
    }
    async function ConnectDB() {
        return new Promise(async (resolve, reject) => {
            await OpenDB()
                .then(async () => {
                    await ReadDB()
                        .then(() => {
                            console.log('YEAY FINALLY');
                            resolve();
                        })
                })
                .catch((error) => {
                    console.log(error);
                    reject();
                });
        });
    }
    React.useEffect(() => {
        (async () => {
            await ConnectDB()
                .then()
                .catch();
        })();
    }, []);
}

日志写道:

LOG  OPEN database: mysqlite.db
LOG  SQLite.open({"name":"mysqlite.db","createFromLocation":"~mysqlite.db","dblocation":"nosync","assetFilename":"~mysqlite.db"})
LOG  new transaction is waiting for open operation
LOG  Phone connected? true, Server connected? true
LOG  OPEN database: mysqlite.db failed, aborting any pending transactions
LOG  [Error: Could not open database]

我尝试了几种方法,但我无法连接到它。

  1. 直接从 www 移动到 assets 文件夹。卸载手机上的应用并重新运行。
  2. 删除SQLite.enablePromise(false);
  3. react-native 链接 react-native-sqlite-storage
  4. cd android && ./gradlew clean
  5. 按照步骤opendatabase call

【问题讨论】:

    标签: android react-native sqlite


    【解决方案1】:

    尝试从root/android/app/src/main/assets/www/mysqlite.db 移动你的安卓文件 -> root/android/app/src/main/assets/mysqlite.db

    【讨论】:

    • 已经根据我在 #1 上尝试过的帖子完成了
    【解决方案2】:

    我终于可以在装有 Android 11 的三星 Galaxy 上运行它了。我在装有 Android 9 的 Redmi6 上进行了尝试,它可以运行。

    我已经删除了包含 SQLite 的 react-native.config.js

    module.exports = {
        dependencies: {
            "react-native-sqlite-storage": {
                platforms: {
                    android: {
                        sourceDir: "../node_modules/react-native-sqlite-storage/platforms/android-native",
                        packageImportPath: "import io.liteglue.SQLitePluginPackage;",
                        packageInstance: "new SQLitePluginPackage()"
                    }
                }
            }
        }
    };
    

    我还删除了MainApplication.java中的导入模块import io.liteglue.SQLitePluginPackage;,数据库终于打开了。

    我不确定这种方式是否绝对。我希望它是暂时的,因为它反对教程中的方式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-13
      • 1970-01-01
      • 2018-08-31
      • 2019-02-14
      • 2022-01-26
      • 2017-07-13
      • 2017-10-11
      • 2020-12-03
      相关资源
      最近更新 更多