Web SQL insert data into multiple rows
Asked Answered
V

1

3

I've tried to insert variables into multiple rows at once in Web SQL database but with all known to me methods I'm getting errors:

("INSERT INTO tab (a,b) VALUES (?,?),(?,?)",[v1,v2,v3,v4])
>> could not prepare statement (1 near ",": syntax error)

("INSERT INTO tab (a,b) VALUES (?,?,?,?)",[v1,v2,v3,v4])
>> could not prepare statement (1 4 values for 2 columns)

("INSERT INTO tab (a,b) VALUES (?,?)",[v1,v2,v3,v4])
>> number of '?' does not match arguments count

Which one is correct for Web SQL and where is my mistake?

Vulva answered 20/10, 2013 at 13:20 Comment(0)
A
4

As table tab has two columns you can specify only two values to be inserted as a row not 4. Following query will work:

("INSERT INTO tab (a,b) VALUES (?,?)",[v1,v2])

You can execute this query multiple times in a single transaction to add multiple rows to improve performance of overall query and ensure integrity. Hope this helps!!!

Amish answered 20/10, 2013 at 13:27 Comment(5)
I know but i want to put many rows in one sql query //thanks, question was not clear - editedVulva
queries are asynchronous, aren't they? So If I'd like to fire callback after all of them I'd need to count how many successed and how many failed. for 1000 entries at once it doesn't seem to be best solutionVulva
Inserting multiple rows in one INSERT statement is not supported in WEB SQL. You can refer to this link #10032105Amish
mhm... :C thanks for answer, I hope it won't be performance bottleneckVulva
Actually, the first syntax works in some implementations, like Safari. See gpickin.com/index.cfm/blog/…Imtiaz

© 2022 - 2024 — McMap. All rights reserved.