Expo React Native项目中出现“无此表 - SQLite”的错误

7

我想在我的Expo React Native项目中使用SQLite。因此,我下载了SQLite的DB浏览器,并创建了数据库和表格,然后再将它们加载到我的项目中。

现在我有了预先填充好的数据库,我想要将其加载到我的项目中。我遇到了一些问题,比如这个数据库不存在等。但是我通过创建一个数据库模块解决了这个问题:

// Database.js
import * as SQLite from 'expo-sqlite';

module.exports = SQLite.openDatabase('myDb.db');

然后将其导入到“Screens”文件夹中的我的主页:

import Database from '../db/database';

  function myFunction(){
    console.log("Enter Function");
    console.log(Database);
  Database.transaction(function(txn) {
    console.log("Enter Transaction");
  txn.executeSql(
    "INSERT INTO users (name, email, password) VALUES ('Lama', 'lama@gmail,com', 'lam123');",  
  ); //end txn
}, function (error){
  console.log(error);
},
function(){
  console.log("Success");
});

}//end myFunction()

控制台记录:
Enter Function
WebSQLDatabase {
  "_currentTask": null,
  "_db": SQLiteDatabase {
    "_closed": false,
    "_name": "myDb.db",
  },
  "_running": false,
  "_txnQueue": Queue {
    "length": 0,
  },
  "exec": [Function anonymous],
  "version": "1.0",
}
Enter Transaction
Error code 1: no such table: users
- node_modules/expo-sqlite/build/SQLite.js:36:15 in _deserializeResultSet
* [native code]:null in map
- node_modules/expo-sqlite/build/SQLite.js:16:40 in SQLiteDatabase#exec
- node_modules/promise/setimmediate/core.js:37:14 in tryCallOne
- node_modules/promise/setimmediate/core.js:123:25 in setImmediate$argument_0
- node_modules/react-native/Libraries/Core/Timers/JSTimers.js:146:14 in _callTimer
- node_modules/react-native/Libraries/Core/Timers/JSTimers.js:194:17 in _callImmediatesPass
- node_modules/react-native/Libraries/Core/Timers/JSTimers.js:458:30 in callImmediates
* [native code]:null in callImmediates
- node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:407:6 in __callImmediates
- node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:143:6 in __guard$argument_0
- node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:384:10 in __guard
- node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:142:17 in __guard$argument_0
* [native code]:null in flushedQueue
* [native code]:null in invokeCallbackAndReturnFlushedQueue

我不太清楚我在这里缺少什么,我已经尝试了创建另一个数据库的代码并且它可以添加记录(虽然我找不到数据库在我的笔记本电脑中的位置)。我还尝试从DB浏览器向myDb添加记录并且它也可以正常工作: DB browser for SQLite - users table records

我发现以前有人问过这个问题,但是没有帮助性的答案: SQLite Expo API, no such table in pre-populated file?

提前感谢!

1个回答

2

i guess that

SQLite.openDatabase('myDb.db');

打开一个名为myDb.db的数据库,该数据库位于应用程序文件系统下的路径${FileSystem.documentDirectory}SQLite/myDb.db中。 因此,如果没有名为“myDB.db”的文件,sqlite将创建一个同名的新数据库,这就是为什么您无法在新创建的数据库中找到表users的原因。

因此,为确保填充的数据库位于正确的路径中,您需要使用FileSytem将其下载到应用程序中,

因此,您可能需要执行以下操作:

FileSystem.downloadAsync(
        Asset.fromModule(require(path)).uri,
        `${FileSystem.documentDirectory}SQLite/myDb.db`
    )
    .then(function(){



        var db = SQLite.openDatabase('db.db')

        // do whatever else you need here


        })
        .catch(error =>{
            console.error(error)
        })

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接