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?
On sqlite 3.7.2:
and so on
fearless_fool has a great answer for older versions. I just wanted to add that you need to make sure you have all the columns listed. So if you have 3 columns, you need to make sure select acts on 3 columns.
Example: I have 3 columns but I only want to insert 2 columns worth of data. Assume I don't care about the first column because it's a standard integer id. I could do the following...
Note: Remember the "select ... union" statement will lose the ordering. (From AG1)
I am able to make the query dynamic. This is my table:
CREATE TABLE "tblPlanner" ("probid" text,"userid" TEXT,"selectedtime" DATETIME,"plannerid" TEXT,"isLocal" BOOL,"applicationid" TEXT, "comment" TEXT, "subject" TEXT)
and I'm getting all data through a
JSON
, so after getting everything inside anNSArray
I followed this:And finally the output query is this:
which is running well through code also and I'm able to save everything in SQLite successfully.
Before this i made
UNION
query stuff dynamic but that started giving some syntax error. Anyways, this is running well for me.I'm surprised that no one has mentioned prepared statements. Unless you are using SQL on its own and not within any other language, then I would think that prepared statements wrapped in a transaction would be the most efficient way of inserting multiple rows.
Sqlite3 can't do that directly in SQL except via a SELECT, and while SELECT can return a "row" of expressions, I know of no way to make it return a phony column.
However, the CLI can do it:
If you do put a loop around an INSERT, rather than using the CLI
.import
command, then be sure to follow the advice in the sqlite FAQ for INSERT speed: