Reputation: 35
I have a sqlite3 data base in raspberry-pi, I have an script which sends data to the mongo db server. As soon as I send the data to the mongoDB I want to delete the row. But I am not able to do that, here is the code for the following
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();
When I run this I get a "errno":21,"code":"SQLITE_MISUSE"
Upvotes: 1
Views: 9165
Reputation: 7736
Use the completion callback of the each
function to carry out the next command. Then close the database after the delete has been completed.
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();
});
});
});
});
Upvotes: 2