Is it possible to insert multiple rows at a time in an SQLite database?

后端 未结 24 2865
猫巷女王i
猫巷女王i 2020-11-21 06:12

In MySQL you can insert multiple rows like this:

INSERT INTO \'tablename\' (\'column1\', \'column2\') VALUES
    (\'data1\', \'data2\'),
    (\'data1\', \'da         


        
相关标签:
24条回答
  • 2020-11-21 06:41

    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.

    BEGIN TRANSACTION;
    INSERT INTO table VALUES (1,1,1,1);
    INSERT INTO table VALUES (2,2,2,2);
    ...
    COMMIT;
    
    0 讨论(0)
  • INSERT INTO TABLE_NAME 
                (DATA1, 
                 DATA2) 
    VALUES      (VAL1, 
                 VAL2), 
                (VAL1, 
                 VAL2), 
                (VAL1, 
                 VAL2), 
                (VAL1, 
                 VAL2), 
                (VAL1, 
                 VAL2), 
                (VAL1, 
                 VAL2), 
                (VAL1, 
                 VAL2), 
                (VAL1, 
                 VAL2); 
    
    0 讨论(0)
  • 2020-11-21 06:43

    As of version 3.7.11 SQLite does support multi-row-insert. Richard Hipp comments:

    I'm using 3.6.13

    I command like this:

    insert into xtable(f1,f2,f3) select v1 as f1, v2 as f2, v3 as f3 
    union select nextV1+, nextV2+, nextV3+
    

    With 50 records inserted at a time, it takes only a second or less.

    It's true using sqlite to insert multiple rows at a time is very possible. By @Andy wrote.

    thanks Andy +1

    0 讨论(0)
  • 2020-11-21 06:45

    Start from version 2012-03-20 (3.7.11), sqlite support the following INSERT syntax:

    INSERT INTO 'tablename' ('column1', 'column2') VALUES
      ('data1', 'data2'),
      ('data3', 'data4'),
      ('data5', 'data6'),
      ('data7', 'data8');
    

    Read documentation: http://www.sqlite.org/lang_insert.html

    PS: Please +1 to Brian Campbell's reply/answer. not mine! He presented the solution first.

    0 讨论(0)
  • 2020-11-21 06:46

    You can't but I don't think you miss anything.

    Because you call sqlite always in process, it almost doesn't matter in performance whether you execute 1 insert statement or 100 insert statements. The commit however takes a lot of time so put those 100 inserts inside a transaction.

    Sqlite is much faster when you use parameterized queries (far less parsing needed) so I wouldn't concatenate big statements like this:

    insert into mytable (col1, col2)
    select 'a','b'
    union 
    select 'c','d'
    union ...
    

    They need to be parsed again and again because every concatenated statement is different.

    0 讨论(0)
  • 2020-11-21 06:46

    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

    insert into mytable (col)
    select 'c'
    union 
    select 'd'
    union 
    select 'a'
    union 
    select 'b';
    

    In the sqlite the data will be store a,b,c,d...

    0 讨论(0)
提交回复
热议问题