我在raspberry-pi中有一个sqlite3数据库,我有一个脚本将数据发送到mongo数据库服务器。一旦我将数据发送到mongoDB,我想删除该行。但我无法做到这一点,以下是
的代码var sqlite3 = require('sqlite3').verbose();
var sqlDB = new sqlite3.Database('Table23.db');
var mongodb = require('mongodb');
var MongoClient = mongodb.MongoClient;
var url = 'mongodb://192.168.0.87:27017';
var winston = require('winston');
var id_1;
winston.remove(winston.transports.Console);
winston.add(
winston.transports.File, {
filename: 'error.log',
level: 'info',
json: true,
timestamp: true,
handleException: true
}
);
sqlDB.serialize(function() {
sqlDB.each("SELECT * FROM Table23", function(err, row) {
var data = {
type: row.TYPE,
id: row.ID,
place: row.Place
};
var id = data.id;
id_1 = id;
MongoClient.connect(url, function(err, db) {
if (err) {
winston.error('Unable to connect'+ err);
} else {
console.log('Connection successful');
var collection = db.collection('testdb');
collection.insert([data], function(err, result) {
if (err) {
winston.error(err);
} else {
console.log('Inserted %d documents into the "Testdb" collection. The documents inserted with "_id" are:', result.length, result);
}
db.close();
});
}
});
sqlDB.run("DELETE FROM Table23 WHERE id=(?)",[id_1], function(err) {
if(err){
winston.error(err);
}
else{
console.log("Successful");
}
});
});
});
sqlDB.close();
当我运行这个时,我得到一个“错误”:21,“code”:“SQLITE_MISUSE”
答案 0 :(得分:1)
使用each
函数的完成回调来执行下一个命令。然后在删除完成后关闭数据库。
var sqlDB = new sqlite3.Database('./abcd')
var id_1;
sqlDB.serialize(() => {
sqlDB.serialize(() => {
var n = 0;
sqlDB.each("SELECT * FROM Table23", function(err, row, i) {
let data = {
type: row.TYPE,
id: row.ID,
place: row.Place
};
var id = data.id;
id_1 = id;
}, ()=> {
sqlDB.run("DELETE FROM Table23 WHERE id=(?)", id_1, function(err) {
if(err){
console.log(err)
}
else{
console.log("Successful");
}
sqlDB.close();
});
});
});
});