如何使用auto_increment主键列将多个数据插入表中?

时间:2021-11-21 09:08:59

So I'm using SQLite 3.6 and tried to insert multiple data to my table which has 3 columns (1 AI PK) using

所以我正在使用SQLite 3.6并试图将多个数据插入到我的表中,该表有3列(1 AI PK)使用

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

somehow it didn't work and I got this error

不知怎的,它没有用,我得到了这个错误

table transport_type has 3 columns but 2 values were supplied

table transport_type有3列,但提供了2个值

Is there any other way to input multiple data?

有没有其他方式输入多个数据?

1 个解决方案

#1


4  

If you are not providing data for all columns in a table, or the data you provide is not in the same order as the columns in the table, you must provide a list of column names:

如果您没有为表中的所有列提供数据,或者您提供的数据与表中的列的顺序不同,则必须提供列名列表:

INSERT INTO tablename(column1, column2)
SELECT ...

Related examples:

相关示例:

  1. Omitting some columns to use their default value.

    省略一些列以使用其默认值。

    Technically this is the same as above, but this demonstrates usage for non-auto-increment columns.

    从技术上讲,这与上面相同,但这表明非自动增量列的用法。

    INSERT INTO myTable(column1, column7, column21)
    SELECT 'one', 'seven', 'twenty-one';
    
  2. Inserting columns in a different order than was defined.

    以不同于已定义的顺序插入列。

    Given a table with the following definition:

    给定一个具有以下定义的表:

    CREATE TABLE myTable (
        column1 VARCHAR(255),
        column2 VARCHAR(255),
        column3 VARCHAR(255),
        column4 VARCHAR(255)
    );
    

    We can specify a column list to insert rows where the data is given out of order. Note the columns are specified in a different order than the definition.

    我们可以指定列列表来插入不按顺序给出数据的行。请注意,列的顺序与定义的顺序不同。

    INSERT INTO myTable(column3, column4, column2, column1)
    SELECT 'three', 'four', 'two', 'one';
    

#1


4  

If you are not providing data for all columns in a table, or the data you provide is not in the same order as the columns in the table, you must provide a list of column names:

如果您没有为表中的所有列提供数据,或者您提供的数据与表中的列的顺序不同,则必须提供列名列表:

INSERT INTO tablename(column1, column2)
SELECT ...

Related examples:

相关示例:

  1. Omitting some columns to use their default value.

    省略一些列以使用其默认值。

    Technically this is the same as above, but this demonstrates usage for non-auto-increment columns.

    从技术上讲,这与上面相同,但这表明非自动增量列的用法。

    INSERT INTO myTable(column1, column7, column21)
    SELECT 'one', 'seven', 'twenty-one';
    
  2. Inserting columns in a different order than was defined.

    以不同于已定义的顺序插入列。

    Given a table with the following definition:

    给定一个具有以下定义的表:

    CREATE TABLE myTable (
        column1 VARCHAR(255),
        column2 VARCHAR(255),
        column3 VARCHAR(255),
        column4 VARCHAR(255)
    );
    

    We can specify a column list to insert rows where the data is given out of order. Note the columns are specified in a different order than the definition.

    我们可以指定列列表来插入不按顺序给出数据的行。请注意,列的顺序与定义的顺序不同。

    INSERT INTO myTable(column3, column4, column2, column1)
    SELECT 'three', 'four', 'two', 'one';