在db.each中执行更新查询

时间:2017-11-25 02:13:56

标签: javascript json ajax sqlite

我正在使用javascript和sqlite,并尝试在成功后的AJAX发布后,在SELECT查询中运行UPDATE查询。 SELECT和AJAX帖子是成功的,但是在UPDATE上,返回错误:错误:SQLITE_BUSY:数据库被锁定

var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database('sqlite-database.db');

db.serialize(function(){

        db.each("SELECT id_sales, id_customer, date FROM sales where syncstatus = 0", function(err, row) {

        id_sales = row.id_sales;
        id_customer = row.id_customer;
        date = row.date;

        $.ajax('http://localhost/newserver/sales/sync_sales', {
            type: 'POST',
            async: false,
            data: { id_sales_ori: id_sales, id_customer:id_customer, date: date },
            success: function (data, status, xhr) {


                var sqlite3 = require('sqlite3').verbose();
                var db = new sqlite3.Database('sqlite-database.db');

            db.run("UPDATE sales set syncstatus = $status WHERE id_sales = $id_sales",{
                $id_sales: id_sales,
                $status: 1,
            },function (err,rows){
                if (err === null){
                console.log("syncstatus update to 1");
                }else{
                console.log(err);
                }
            });

            },
            error: function (jqXhr, textStatus, errorMessage) {
                    console.log("fail sync");
                    stat[id_sales] = 0;
                }
        });        

    });
});
db.close();

请帮我如何在db.each中正确执行UPDATE查询。

0 个答案:

没有答案