node.js - SQLITE_CANTOPEN - nodejs-mobile
问题描述
错误:SQLITE_CANTOPEN
var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database('MyDB.db', sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE);
db.serialize(function() {
db.run("CREATE TABLE lorem (info TEXT)");
var stmt = db.prepare("INSERT INTO lorem VALUES (?)");
for (var i = 0; i < 10; i++) {
stmt.run("Ipsum " + i);
}
stmt.finalize();
db.all("SELECT rowid AS id, info FROM lorem", function(err, rows) {
rn_bridge.channel.send(rows);
});
});
我在以下环境中运行它:https ://github.com/janeasystems/nodejs-mobile react native
在 node.js 上下文中运行没有 rn_bridge.channel.send(rows) 和 console.lognode index.js
工作正常。
解决方案
The issue is likely that sqlite3 is internally using the Current Working Directory for this, which is not a path would be writable in iOS / Android applications.
Using the rn_bridge.app.datadir()
API would be the way to have a writable location.
https://github.com/JaneaSystems/nodejs-mobile-react-native/tree/49c069539db138688a9e44c44180bb0c3335537c#rn_bridgeappdatadir
Something like this would be the way to do it:
const path = require('path');
var sqlite3 = require('sqlite3').verbose();
var db_path= path.join(cordova.app.datadir(), 'MyDB.db');
var db = new sqlite3.Database(db_path, sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE);
推荐阅读
- python - 如何分隔文本文件中的值?
- java - 为什么我在运行时使用 jre websphere 而不是正常的 jre 1.7 工作时会出现这个问题?
- python - Gremlin Python - 基于脚本的方法不返回全部 - JanusGraph
- jsf - 使用 javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL 导致 viewParam 为空
- android - 在 Cloud Firestore 中设置安全规则
- azure - Azure 机器学习工作室表示交叉验证模块“需要值”
- c# - 如何为每个登录用户拥有一个持久数据容器 - ASP.NET Core
- c++-winrt - Win2D如何画半个椭圆?
- php - 如何使用 php 和 mysql 在表中插入新列?
- node.js - 在实际调用我的 passwordMatch 函数之前,Passport 使用“错误密码”进行身份验证失败