I successfully updated sql data by following codes.
app.get('/courtInfo/:courtName',async function(req,res){
let db = await getDBConnection();
let rows = await db.all('select * from courts');
const name = req.params.courtName;
const newPlayerNum = req.query.playerUpdated;
const newIsPickup = req.query.isPickup;
if(typeof newPlayerNum!=='undefined'&&newPlayerNum!=''){
var newsql = 'UPDATE courts SET currentNum = ? WHERE court_name = ?';
db.run(newsql,[newPlayerNum,name],function(err){
if(err) return console.log(err);
});
}
if(typeof newIsPickup!=='undefined'&&newIsPickup!=''){
var newsql = 'UPDATE courts SET is_pickup = ? WHERE court_name =?';
db.run(newsql,[newIsPickup,name],function(err){
if(err) return console.log(err);
});
}
await db.close();
.....
But when I tried like this, I got Sqlite Busy error.
app.post('/newCourtAdded', upload.single('courtImg'),async function(req,res){
let db = await getDBConnection();
let rows = await db.all('select * from courts');
const newCourtId = rows[rows.length-1]+1;
const newCourtName = req.body.courtName;
const newCourtNum = req.body.courtNum;
const newCourtIsWater = req.body.isWater;
const newImagePath = req.file.originalname;
console.log(newCourtName, newCourtNum, newCourtIsWater, newImagePath);
var newSqlite = 'INSERT INTO courts (court_id, court_name, court_image, is_water, is_pickup, court_num, currentNum) VALUES (?,?,?,?,?,?,?)';
var newValue = [];
newValue.push(newCourtId);
newValue.push(newCourtName);
newValue.push('/image/'+newImagePath);
newValue.push(newCourtIsWater);
newValue.push(0);
newValue.push(newCourtNum);
newValue.push(0);
db.run(newSqlite,newValue,function(err){
if(err){
return console.log(err);
}else{
db.close();
}
});
res.redirect("http://localhost:3000");
});
I think they have same logic, I mean, open database -> make dataSet -> change and save database -> close database.
I don't know Why the former one is successful and the other is not. Could you please teach me why?