简体   繁体   English

如何在Web SQL数据库中插入多行?

[英]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. 我有一个包含多行数据的JavaScript对象,我想将其插入到Web sql数据库中。 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 . 这可以通过在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 ? 是否可以在单个查询中添加多行,例如在单个MySQL INSERT添加多个值? Or I should not worry about this. 或者我不应该担心这个。

This can be done by moving outer loop inside db.transaction. 这可以通过在db.transaction中移动外部循环来完成。 Will it be better and why? 它会更好吗?为什么?

yes. 是。 much better. 好多了。 1) creating a transaction is not cheap. 1)创建交易并不便宜。 2) looping async is generally bad . 2)循环异步通常很糟糕。

Is it possible to add multiple rows in single query like multiple values in single MySQL INSERT? 是否可以在单个查询中添加多行,例如在单个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. 再次不要循环executeSql,它是异步的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM