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?

Lapsio
  • 6,384
  • 4
  • 20
  • 26

1 Answers1

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!!!

Deepshikha
  • 9,896
  • 2
  • 21
  • 21
  • I know but i want to put many rows in one sql query //thanks, question was not clear - edited – Lapsio Oct 20 '13 at 13:34
  • 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 solution – Lapsio Oct 20 '13 at 13:39
  • 2
    Inserting multiple rows in one INSERT statement is not supported in WEB SQL. You can refer to this link http://stackoverflow.com/questions/10031605/optimizing-websql-local-database-population/10031717#10031717 – Deepshikha Oct 20 '13 at 13:40
  • mhm... :C thanks for answer, I hope it won't be performance bottleneck – Lapsio Oct 20 '13 at 13:44
  • Actually, the first syntax works in some implementations, like Safari. See http://www.gpickin.com/index.cfm/blog/loading-or-dumping-large-amounts-of-data-into-websql-options-with-performance-numbers – Matthieu Dec 29 '16 at 14:22