基本 React Native SQLite 脚本返回 undefined 导致错误 [英] basic React Native SQLite script returning undefined which causes error

查看:31
本文介绍了基本 React Native SQLite 脚本返回 undefined 导致错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习如何使用 react-native-sqlite-storage,如本页所述:

I'm trying to learn to use react-native-sqlite-storage as described on this page here:

https://github.com/andpor/react-native-sqlite-storage

我正在尝试构建一个 android 应用程序的 Windows 机器上工作.我有以下代码:

I am working on a windows machine trying to build an android app. I have the following code:

import SQLite from 'react-native-sqlite-storage';

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

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

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

var db = SQLite.openDatabase("test.db", "1.0", "Test Database", 200000, openCB, errorCB);
db.transaction((tx) => {
  tx.executeSql('SELECT * FROM Employees a, Departments b WHERE a.department = b.department_id', [], (tx, results) => {
      console.log("Query completed");

    });
});

export default 1;

当我运行这个时,我收到错误

When I run this, I get the error

undefined 不是一个对象(评估'NativeModules["SQLite"][方法]')

undefined is not an object (evaluating 'NativeModules["SQLite"][method]')

错误发生在 var db = SQLite.openDatabase("test.db", "1.0", "Test Database", 200000, openCB, errorCB);

我怀疑发生错误的原因是 SQLite 期望在您实际打开数据库之前存在数据库.但是,我在上面的链接中没有看到有关如何使用 react-native-sqlite-storage 创建数据库的任何说明,所以我不确定我的假设是否正确.此外,我假设我需要为员工创建一个表,但同样,上面的链接没有明确说明如何创建一个表的示例.所以我不知道我的假设是否正确.

I suspect the reason the error is happening is because SQLite expects the existence of a database before you can actually open it. However, I do not see any instructions in the link above on how to create a database with react-native-sqlite-storage, so I'm not sure if my assumption is correct. Additionally, I assume I'd need to have a table for Employees, but again, the link above didn't explicitly state examples on how to create one. SO I don't know if my assumption is correct.

那么如何让 SQLite.openDatabase() 命令起作用?

So how do I get SQLite.openDatabase() command to work?

更多信息

我删除了我的项目.然后我尝试再次重新安装所有东西,这次严格按照andpor的github说明中提到的android的sqlite存储安装说明(即修改gradle文件,更新MainApplication类等).一旦我这样做了,从 CLI 运行命令 react-native run-android 会导致此构建错误:

I deleted my project. Then I tried to re-install everything again, this time closely following the installation instructions for sqlite storage for android as mentioned on the andpor's github instructions (ie, modifying gradle files, updating MainApplication class etc...). Once I did that, running the command react-native run-android from the CLI causes this build error:

ERROR EPERM: operation not allowed, lstat 'C:\Users\JohnLai\Projects\ReactNative\myapp\android\app\build\intermediates\exploded-aar\myapp\realm\unspecified\jars\classes.jar'{"errno":-4048,"code":"EPERM","syscall":"lstat","path":"C:\Users\JohnLai\Projects\ReactNative\myapp\android\app\build\intermediates\exploded-aar\myapp\realm\unspecified\jars\classes.jar"}错误:EPERM:不允许操作,lstat 'C:\Users\JohnLai\Projects\ReactNative\myapp\android\app\build\intermediates\exploded-aar\myapp\realm\unspecified\jars\classes.jar'在错误(本机)

ERROR EPERM: operation not permitted, lstat 'C:\Users\John Lai\Projects\ReactNative\myapp\android\app\build\intermediates\exploded-aar\myapp\realm\unspecified\jars\classes.jar' {"errno":-4048,"code":"EPERM","syscall":"lstat","path":"C:\Users\John Lai\Projects\ReactNative\myapp\android\app\build\intermediates\exploded-aar\myapp\realm\unspecified\jars\classes.jar"} Error: EPERM: operation not permitted, lstat 'C:\Users\John Lai\Projects\ReactNative\myapp\android\app\build\intermediates\exploded-aar\myapp\realm\unspecified\jars\classes.jar' at Error (native)

我尝试以普通用户和管理员身份运行此命令,但仍然是同样的问题.

I tried running this command as a regular user and as an administrator, and still the same issue.

更多信息 2

我删除了我的项目.然后我为我所有的项目做了 npm install.然后我尝试了命令 rnpm linkreact-native link react-native-sqlite-storage,它们都给出了这个错误:

I deleted my project. Then I did npm install for all my projects. Then I tried the commands rnpm link and react-native link react-native-sqlite-storage, both of which gave this error:

错误!链接时似乎出了点问题.错误:产生未知请在此处提交问题:https://github.com/rnpm/rnpm/issues

ERR! It seems something went wrong while linking. Error: spawn UNKNOWN Please file an issue here: https://github.com/rnpm/rnpm/issues

所以现在该项目甚至无法构建.有什么想法吗?

So now the project won't even build. Any ideas?

推荐答案

您需要将 MainApplication.java 更改为:

You need change MainApplication.java with:

导入 org.pgsqlite.SQLitePluginPackage;

import org.pgsqlite.SQLitePluginPackage;

在 getPackages 中:

in getPackages:

新的 SQLitePluginPackage()

new SQLitePluginPackage()

这篇关于基本 React Native SQLite 脚本返回 undefined 导致错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆