In MySQL you can insert multiple rows like this:
INSERT INTO 'tablename' ('column1', 'column2') VALUES
('data1', 'data2'),
('data1', 'data2'),
('data1', 'data2'),
('data1', 'data2');
However, I am getting an error when I try to do something like this. Is it possible to insert multiple rows at a time in an SQLite database? What is the syntax to do that?
I wrote some ruby code to generate a single 500 element multi-row insert from a series of insert statements which was considerably faster than running the individual inserts. Then I tried simply wrapping the multiple inserts into a single transaction and found that I could get the same kind of speed up with considerably less code.
I have a query like below, but with ODBC driver SQLite has an error with "," it says. I run vbscript in HTA (Html Application).
Yes, as of SQLite 3.7.11 this is supported in SQLite. From the SQLite documentation:
(when this answer was originally written, this was not supported)
For compatibility with older versions of SQLite, you can use the trick suggested by andy and fearless_fool using
UNION
, but for 3.7.11 and later the simpler syntax described in here should be preferred.Yes, sql can do this, but with a different syntax. The sqlite documentation is pretty good, by the way. It will also tell you that the only way to insert several row is use a select statement as the source of the data to be inserted.
The problem with using transaction is that you lock the table also for reading. So if you have really much data to insert and you need to access to your data, for exemple a preview or so, this way doesn't work well.
The problem with the other solution is that you lose the order of the inserting
In the sqlite the data will be store a,b,c,d...
If you are using bash shell you can use this:
Or if you are in sqlite CLI, then you need to do this:
How does it work? It makes use of that if table
tab
:then
select a.id, b.id from tab a, tab b
returnsand so on. After first execution we insert 2 rows, then 2^3=8. (three because we have
tab a, tab b, tab c
)After second execution we insert additional
(2+8)^3=1000
rowsAftern thrid we insert about
max(1000^3, 5e5)=500000
rows and so on...This is the fastest known for me method of populating SQLite database.