react-native - 初始化 SQLite 数据库
问题描述
我在以下代码中使用“react-native-sqlite-storage”
import SQLite from 'react-native-sqlite-storage';
SQLite.DEBUG(true);
SQLite.enablePromise(true);
//"test.db", "1.0", "Test Database", 200000, openCB, errorCB)
const db = SQLite.openDatabase({name: 'mydb.db'});
//'CREATE TABLE IF NOT EXISTS transactions (id INTEGER PRIMARY KEY NOT NULL, date TEXT NOT NULL, start TEXT NOT NULL, end TEXT NOT NULL);'
export const initdb = () => {
const promise = new Promise((resolve, reject) => {
db.transaction(tx => {
tx.executeSql(
'CREATE TABLE IF NOT EXISTS transactions (id INTEGER PRIMARY KEY NOT NULL, date TEXT NOT NULL, start TEXT NOT NULL, end TEXT NOT NULL);',
[],
() => {
resolve();
},
(_, err) => {
reject(err);
},
);
});
});
return promise;
};
我正在使用以下代码
initdb()
.then(() => {
console.log('Sucessfully Initialised Databse ');
})
.catch(error => {
console.log('Failed Database');
console.log(error);
});
我怎么在日志中得到这个错误
LOG Promise based runtime ready
LOG OPEN database: mydb.db
LOG SQLite.open({"name":"mydb.db","dblocation":"nosync"})
LOG Failed Database
LOG [TypeError: undefined is not a function (near '...db.transaction...')]
LOG Running "timekeep" with {"rootTag":1,"initialProps":{}}
我在ios模拟器上试试
解决方案
推荐阅读
- javascript - 将 SVG D3 js 导出为 PNG 或 JPEG 并通过电子邮件发送
- tableau-api - 如何在 TABLEAU 中组合创建三个条形图和折线图
- react-native - 从 Web 视图打开带有 PHAsset URI 的文件
- performance - 每次运行代码都会变慢 (PowerPoint VSTO)
- javascript - 尝试在 Django 应用程序的 js 文件中导入 vue.js 时出现“未捕获的语法错误:无法在模块外使用 import 语句”
- sql - 加入两个表以获取匹配的记录和不匹配的记录
- html - 反应中文本输入的动态颜色
- javafx - 如何在 JavaFX (JFoenix) 中显示类似 Android 的吐司消息?
- kubernetes - 我可以在不通过 Terraform 声明任何服务的情况下通过 Terrafrom 设置 Kubernetes Ingress 吗?
- python - Python Pandas,根据另一列中的值进行日期时间舍入