最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to insert multiple rows in Web SQL Database? - Stack Overflow

programmeradmin10浏览0评论

I have a JavaScript object with multiple rows of data and I want to insert it into web sql database. Here is how my code looks like.

for(i in rows)
{
    (function(row){
        db.transaction(function(tx) {
            tx.executeSql("INSERT INTO my_table (id, name, parent_id) VALUES (?, ?, ?)",
                [ row.id, row.name, row.parent_id ], onSuccess, onError
            );
        });
    })(rows[i]);
}

My questions about this are:

  1. This can be done by moving outer loop inside db.transaction. Will it be better and why?
  2. Is it possible to add multiple rows in single query like multiple values in single MySQL INSERT? Or I should not worry about this.

I have a JavaScript object with multiple rows of data and I want to insert it into web sql database. Here is how my code looks like.

for(i in rows)
{
    (function(row){
        db.transaction(function(tx) {
            tx.executeSql("INSERT INTO my_table (id, name, parent_id) VALUES (?, ?, ?)",
                [ row.id, row.name, row.parent_id ], onSuccess, onError
            );
        });
    })(rows[i]);
}

My questions about this are:

  1. This can be done by moving outer loop inside db.transaction. Will it be better and why?
  2. Is it possible to add multiple rows in single query like multiple values in single MySQL INSERT? Or I should not worry about this.
Share Improve this question edited Nov 14, 2012 at 19:50 Musa 97.8k17 gold badges122 silver badges143 bronze badges asked Nov 14, 2012 at 19:46 NaveedNaveed 1,22111 silver badges22 bronze badges 3
  • purely sql options: blog.sqlauthority./2012/08/29/… – Stefan Commented Nov 14, 2012 at 19:50
  • stackoverflow./questions/452859/… – Stefan Commented Nov 14, 2012 at 19:51
  • possible duplicate of Web SQL insert data into multiple rows – Chepech Commented Apr 24, 2014 at 22:16
Add a ment  | 

1 Answer 1

Reset to default 3

This can be done by moving outer loop inside db.transaction. Will it be better and why?

yes. much better. 1) creating a transaction is not cheap. 2) looping async is generally bad .

Is it possible to add multiple rows in single query like multiple values in single MySQL INSERT? Or I should not worry about this.

don't worry. Multiple rows workaround are syntactic sugar. No performance benefit. Better loop it under one transaction.

Again do not loop executeSql, it is async.

发布评论

评论列表(0)

  1. 暂无评论