Cordova SQLite多条记录可插入所需的建议

时间:2015-12-06 21:11:10

标签: sqlite cordova

我正在学习我在cordova周围的方式,我正在使用visual studio,并且对如何编写插入函数以将多个记录添加到sqlite数据库感到困惑。我可以为一条记录做到这一点没有任何问题,但是当我尝试为多个记录执行此操作时,事情会稍微误入歧途。

我有这个代码,我知道这不是很好,但正如我说我正在学习所以试着建立在我所学到的东西上找到合适的解决方案。

 $.ajax({
    type: "post",
    url: baseURI
}).then(function (r) {
    var items = [];
    var db = window.openDatabase("BlissData", "1.0", "Bliss Data", 200000);
    $.each(r.aaData, function (line) {
        localStorage.customerid = r.aaData[line].Customerid;
        localStorage.customername = r.aaData[line].CustomerName;
        localStorage.address1 = r.aaData[line].siteAddress1;
        localStorage.address2 = r.aaData[line].siteAddress2;
        localStorage.address3 = r.aaData[line].siteAddress3;
        localStorage.address4 = r.aaData[line].siteAddress4;
        localStorage.mobile = r.aaData[line].mobilephone;
        localStorage.telephone = r.aaData[line].Telephone;
        localStorage.email = r.aaData[line].Email;
        db.transaction(insertCustomers, errorCB);
    });
});

function insertCustomers(tx){

tx.executeSql('INSERT INTO BlissCustomers (CustomerId,CustomerName,Address1,Address2,Address3,Address4,Telephone,Mobile,Email) VALUES ("' + localStorage.customerid + '","' + localStorage.customername + '","' + localStorage.address1 + '","' + localStorage.address2 + '","' + localStorage.address3 + '","' + localStorage.address4 + '","' + localStorage.mobile + '","' + localStorage.telephone + '","' + localStorage.email + '")',[],nullHandler,errorHandler);

}

当执行此操作时,我将10行添加到表中,但所有行都具有相同的数据。所以我想这里有一个理解问题!

然后我找到了一个看起来更好的解决方案的不同例子。但是,这会返回错误 - 未捕获的ReferenceError:未定义dbExecution

db.transaction(function(tx){

        $.each(r.aaData, function (line) {
            dbExecution.databaseVar.transaction(function (tx) {
                tx.executeSql("INSERT INTO BlissCustomers (CustomerId,CustomerName,Address1,Address2,Address3,Address4,Telephone,Mobile,Email) VALUES (?,?)", [r.aaData[line].Customerid, r.aaData[line].CustomerName, r.aaData[line].siteAddress1, r.aaData[line].siteAddress2, r.aaData[line].siteAddress3, r.aaData[line].siteAddress4, r.aaData[line].mobilephone, r.aaData[line].telephone, r.aaData[line].email]);
            });
        }, error, success);

    });

在任何地方都有一个简单的教程,我可以阅读以了解我做错了什么,或者有人能指出我在正确的方向吗?

1 个答案:

答案 0 :(得分:0)

您的问题是$.each循环的每个循环都会打开一个新事务,而不会等待前一个事务完成。您可以将事务链接在一起,等待每个事务完成,然后再转到下一个事务,但是将一个tranasction中的所有数据插入到单个SQL块中会更有效。例如:

$.ajax({
    type: "post",
    url: baseURI
}).then(function (r) {
    var sql = "";
    var db = window.openDatabase("BlissData", "1.0", "Bliss Data", 200000);
    $.each(r.aaData, function (line, row) {
        sql += 'INSERT INTO BlissCustomers (CustomerId,CustomerName,Address1,Address2,Address3,Address4,Telephone,Mobile,Email) VALUES ("' + row.Customerid + '","' + row.CustomerName + '","' + row.siteAddress1 + '","' + row.siteAddress2 + '","' + row.siteAddress3 + '","' + row.siteAddress4 + '","' + row.mobilephone + '","' + row.Telephone + '","' + row.Email + '");';
    });
    db.transaction(insertCustomers.bind(this, sql), errorCB);
});

function insertCustomers(sql, tx) {
    tx.executeSql(sql,[],successHandler,errorHandler);
}

如果您有大量数据,那么在SQL插入(see this SO answer for details]中使用UNION SELECT语法会更有效。

您可以考虑使用cordova-sqlite-porter这是我创建的一个小插件,用于将数据导入/导出到Cordova项目中的SQLite数据库中。您可以传递JSON结构,它将以最佳方式执行插入。例如:

$.ajax({
    type: "post",
    url: baseURI
}).then(function (r) {
    var items = [], item;
    var db = window.openDatabase("BlissData", "1.0", "Bliss Data", 200000);
    $.each(r.aaData, function (line, row) {
        item = {};
        item.CustomerId = row.Customerid;
        item.CustomerName = row.CustomerName;
        item.Address1 = row.siteAddress1;
        item.Address2 = row.siteAddress2;
        item.Address3 = row.siteAddress3;
        item.Address4 = row.siteAddress4;
        item.Mobile = row.mobilephone;
        item.Telephone = row.Telephone;
        item.Email = row.Email;
        items.push(item);
    });

    var json = {
        "data":{
            "inserts":{
                "BlissCustomers": items
            }
        }
    };

    cordova.plugins.sqlitePorter.importJsonToDb(db, json, {
        successFn: successFn,
        errorFn: errorFn
    });
});