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

后端 未结 24 2862
猫巷女王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:32

    I have a query like below, but with ODBC driver SQLite has an error with "," it says. I run vbscript in HTA (Html Application).

    INSERT INTO evrak_ilac_iliskileri (evrak_id, ilac_id, baglayan_kullanici_id, tarih) VALUES (4150,762,1,datetime()),(4150,9770,1,datetime()),(4150,6609,1,datetime()),(4150,3628,1,datetime()),(4150,9422,1,datetime())
    
    0 讨论(0)
  • 2020-11-21 06:32

    On sqlite 3.7.2:

    INSERT INTO table_name (column1, column2) 
                    SELECT 'value1', 'value1' 
              UNION SELECT 'value2', 'value2' 
              UNION SELECT 'value3', 'value3' 
    

    and so on

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

    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:

    .import FILE TABLE     Import data from FILE into TABLE
    .separator STRING      Change separator used by output mode and .import
    
    $ sqlite3 /tmp/test.db
    SQLite version 3.5.9
    Enter ".help" for instructions
    sqlite> create table abc (a);
    sqlite> .import /dev/tty abc
    1
    2
    3
    99
    ^D
    sqlite> select * from abc;
    1
    2
    3
    99
    sqlite> 
    

    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:

    By default, each INSERT statement is its own transaction. But if you surround multiple INSERT statements with BEGIN...COMMIT then all the inserts are grouped into a single transaction. The time needed to commit the transaction is amortized over all the enclosed insert statements and so the time per insert statement is greatly reduced.

    Another option is to run PRAGMA synchronous=OFF. This command will cause SQLite to not wait on data to reach the disk surface, which will make write operations appear to be much faster. But if you lose power in the middle of a transaction, your database file might go corrupt.

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

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

    INSERT INTO 'tablename'
          SELECT NULL AS 'column1', 'data1' AS 'column2', 'data2' AS 'column3'
    UNION SELECT NULL, 'data3', 'data4'
    UNION SELECT NULL, 'data5', 'data6'
    UNION SELECT NULL, 'data7', 'data8'
    

    Note: Remember the "select ... union" statement will lose the ordering. (From AG1)

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

    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.

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

    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.

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