How to insert multiple rows in Web SQL Database?
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:
db.transaction
. Will it be better and why? INSERT
? Or I should not worry about this. 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.
链接地址: http://www.djcxy.com/p/94510.html上一篇: 只需一个插入通讯插入一个表中的多行
下一篇: 如何在Web SQL数据库中插入多行?