从ios打开react-native-sqlite-storage数据库时,无法读取未定义的属性“open”

ddarikpa  于 2023-05-23  发布在  SQLite
关注(0)|答案(3)|浏览(230)

我尝试使用react-native-sqlite-storage来处理react native for ios上的本地数据库。

var SQLite = require('react-native-sqlite-storage');
var db = []; 

export default class DataBase {
  constructor () {
    db = SQLite.openDatabase("eventHelper.db", "1.0", "EventHelper", 200000, this.openCB, this.errorCB);
  }

  errorCB(err) {
    console.log("SQL Error: " + err);
  }

  successCB() {
    console.log("SQL executed fine");
  }

  openCB() {
    console.log("Database OPENED");
  }
}

但我得到以下错误消息:无法读取未定义的属性“open”。我不知道openDatabase是如何工作的,我还没有创建数据库。我不确定是否应该先创建数据库,以及如何创建,或者如果数据库不存在,openDatabase是否会这样做。
我查找了这个问题,并尝试了这里建议的解决方案https://github.com/andpor/react-native-sqlite-storage/issues/64-uninstall react-native-sqlite-storage - removed the node_modules folder - npm install - npm install --保存react-native-sqlite-storage - rnpm link
但我的构建失败x1c 0d1x
任何建议都将受到高度赞赏。谢谢!

vsnjm48y

vsnjm48y1#

安装SQLite插件后,您是否重新运行了react-native run-ios?我得到了同样的错误,并修复了它,希望它有帮助

i34xakig

i34xakig2#

一个简单的使用示例(目前我正在一个生产应用中使用它)

var SQLite = require('react-native-sqlite-storage');
var db = SQLite.openDatabase({name : "db.sqlite", location: 'default'});

db.executeSql("SELECT * FROM users WHERE userName=?",['user1'],
            (results) => {
              console.log(results);
            },
            (err) => {
              console.log(err);
            }
            );
8qgya5xd

8qgya5xd3#

如果您在应用程序中使用Expo,则会因为导入冲突而发生此问题。
要解决此问题,只需执行以下操作:

import * as SQLite from "expo-sqlite";
//and when you want to initialize your db
const db = SQLite.openDatabase("test.db");

而不是:

import SQLite from "react-native-sqlite-storage"

希望这能帮助任何仍然面临这个问题的人。

相关问题