没有这样的表 - Expo React Native 项目中的 SQLite

Lam*_*ama 7 sqlite react-native expo

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

现在我已经有了预填充的数据库,我想将其加载到我的项目中。我遇到了几个问题,比如这个数据库不存在......等等。不过我通过数据库模块解决了这个问题:

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

module.exports = SQLite.openDatabase('myDb.db');
Run Code Online (Sandbox Code Playgroud)

然后将其导入到我的主页中的 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()
Run Code Online (Sandbox Code Playgroud)

控制台记录:

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
Run Code Online (Sandbox Code Playgroud)

我真的不知道我在这里缺少什么,我已经尝试过创建另一个数据库的代码,它可以工作并添加记录(尽管我找不到数据库在我的笔记本电脑中的位置)。我还尝试将数据库浏览器中的记录添加到 myDb 并且它有效: SQLite 的数据库浏览器 - 用户表记录

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

先感谢您!

小智 2

我猜可能是

SQLite.openDatabase('myDb.db');
Run Code Online (Sandbox Code Playgroud)

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

因此,为了确保填充的数据库位于正确的路径中,您需要使用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)
        })
Run Code Online (Sandbox Code Playgroud)