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

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

提交回复
热议问题