【问题标题】:No such table - SQLite in Expo react native project没有这样的表-世博会中的 SQLite 反应原生项目
【发布时间】:2020-05-17 04:20:30
【问题描述】:

我想在我的 Expo react 原生项目中使用 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

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

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

提前谢谢你!

【问题讨论】:

    标签: sqlite react-native expo


    【解决方案1】:

    我猜

    SQLite.openDatabase('myDb.db');
    

    正在打开一个名为 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)
            })
    

    【讨论】:

      猜你喜欢
      • 2022-06-14
      • 2021-03-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-23
      • 1970-01-01
      • 2020-05-07
      • 1970-01-01
      • 2019-04-09
      相关资源
      最近更新 更多