如何在SQLite中记录错误回调?

如何在SQLite中记录错误回调?,sqlite,react-native,Sqlite,React Native,我正在尝试使用以下代码连接到数据库: import SQLite from 'react-native-sqlite-storage' var db = SQLite.openDatabase({name : "banco.db", createFromLocation : 1}, this.successCB(), this.errorCB()); errorCB() { this.setState({message: "I NEED SHOW THE ERROR HERE"}); }

我正在尝试使用以下代码连接到数据库:

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

var db = SQLite.openDatabase({name : "banco.db", createFromLocation : 1}, this.successCB(), this.errorCB());

errorCB() {
  this.setState({message: "I NEED SHOW THE ERROR HERE"});
} 

successCB() {
  this.setState({message: "SQL executed fine"});
}
如何在
errorCB
功能上显示错误?

这是一个。错误回调将传递一个包含错误的参数。您也没有向
openDatabase
提供正确的值。您应该传入函数,而不是试图调用函数

复制粘贴文档中的相关部分,并附上注释以解释:

// Your error callback function that should take an argument that will contain your error.
errorCB(err) {
  console.log("SQL Error: " + err);
  // Here you can use err in your setState call.
}

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

// openDatabase should be passed in the functions; openCB and errorCB in this example.
var db = SQLite.openDatabase("test.db", "1.0", "Test Database", 200000, openCB, errorCB);

// What you're doing is incorrect as it's akin to doing this which is wrong.
// var db = SQLite.openDatabase("test.db", "1.0", "Test Database", 200000, openCB(), errorCB());
这实际上是一个基本的JavaScript问题,您需要能够阅读文档并理解如何使用给定的API。如果您在这方面遇到问题,我建议您仔细阅读,因为它是JavaScript的基础

编辑:非常直接地回答评论;您的代码应该是这样的:

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

var db = SQLite.openDatabase({name : "banco.db", createFromLocation : 1}, this.successCB, this.errorCB);

// Making the assumption that these are in a class,
// otherwise add the const keyword before them.
// Convert these to arrow functions instead
// so they can more easily be passed as variables.
errorCB = (err) => {
  this.setState({message: err});
}

successCB = () => {
  this.setState({message: "SQL executed fine"});
}
考虑到你的意见,我在这里会非常直接。如果您不了解函数、高阶函数和变量/值在JavaScript中是如何工作的,那么使用React Native将非常困难。尤其是当您不熟悉ES6语法时。在处理React Native之前,请先浏览该链接中的书或其他许多学习JavaScript基础知识的重要资源之一。

这在中。错误回调将传递一个包含错误的参数。您也没有向
openDatabase
提供正确的值。您应该传入函数,而不是试图调用函数

复制粘贴文档中的相关部分,并附上注释以解释:

// Your error callback function that should take an argument that will contain your error.
errorCB(err) {
  console.log("SQL Error: " + err);
  // Here you can use err in your setState call.
}

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

// openDatabase should be passed in the functions; openCB and errorCB in this example.
var db = SQLite.openDatabase("test.db", "1.0", "Test Database", 200000, openCB, errorCB);

// What you're doing is incorrect as it's akin to doing this which is wrong.
// var db = SQLite.openDatabase("test.db", "1.0", "Test Database", 200000, openCB(), errorCB());
这实际上是一个基本的JavaScript问题,您需要能够阅读文档并理解如何使用给定的API。如果您在这方面遇到问题,我建议您仔细阅读,因为它是JavaScript的基础

编辑:非常直接地回答评论;您的代码应该是这样的:

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

var db = SQLite.openDatabase({name : "banco.db", createFromLocation : 1}, this.successCB, this.errorCB);

// Making the assumption that these are in a class,
// otherwise add the const keyword before them.
// Convert these to arrow functions instead
// so they can more easily be passed as variables.
errorCB = (err) => {
  this.setState({message: err});
}

successCB = () => {
  this.setState({message: "SQL executed fine"});
}

考虑到你的意见,我在这里会非常直接。如果您不了解函数、高阶函数和变量/值在JavaScript中是如何工作的,那么使用React Native将非常困难。尤其是当您不熟悉ES6语法时。在处理React Native之前,请阅读该链接中的书或其他许多学习JavaScript基础知识的重要资源之一。

我曾经使用以下语句打开SQLite

database = SQLite.openDatabase({name: 'my.db', location: 'default'}, (db) => {
     db.transaction( tx => {
     tx.executeSql(`CREATE TABLE IF NOT EXISTS tableName (columnNames)`);
        }, error => {
           this.setState({message: error});
     });
 }, error => {
    this.setState({message: error});
});

希望这会有帮助

我曾经使用以下语句打开SQLite

database = SQLite.openDatabase({name: 'my.db', location: 'default'}, (db) => {
     db.transaction( tx => {
     tx.executeSql(`CREATE TABLE IF NOT EXISTS tableName (columnNames)`);
        }, error => {
           this.setState({message: error});
     });
 }, error => {
    this.setState({message: error});
});

希望这会有帮助

openCB
errorCB
不是变量,所以我在使用它时收到一个错误,我不想写我的自定义错误,我想得到database@ItaloRodrigo我已经添加了您应该使用的确切代码。您应该真正了解JavaScript基础知识,因为将函数用作变量是编写JavaScript的核心概念。在React Native中,这一点尤其重要,因为其中很多都是纯JavaScript。
openCB
errorCB
不是变量,所以我在使用它时收到一个错误,我不想编写自定义错误,我想得到database@ItaloRodrigo我已经添加了您应该使用的确切代码。您应该真正了解JavaScript基础知识,因为将函数用作变量是编写JavaScript的核心概念。在React Native中,这一点尤其重要,因为其中很多都是纯JavaScript。