Optimizing WebSQL Local Database Population

前端 未结 1 1668
甜味超标
甜味超标 2020-12-10 00:17

I am trying to optimize the speed that my Local database populates in a Web app that is being developed. Currently, it uses PHP to access the Database and then inserts tha

1条回答
  •  攒了一身酷
    2020-12-10 00:33

    I would prepare a query with placeholders, then execute it for each row with the right arguments. Something like this (JS part only, using underscore.js for array helpers):

    db.transaction(function(tx) {
        var q = 'INSERT INTO Cost_Codes (Cost_Code_No, Name, Unit_Of_Measure) VALUES (?, ?, ?)';
        _(rows).each(function(row) {
            tx.executeSql(q, [row.code, row.name, row.unit]);
        });
    });
    

    Edit: a query with placeholders has two main benefits:

    1. It makes it a lot easier for the DB engine to cache and reuse query plans (because you are running the same query a hundred times instead of a hundred different queries once).
    2. It makes escaping data and avoiding SQL injections a lot easier.

    0 讨论(0)
提交回复
热议问题