Import csv into sqlite with autoincrementing primary key

前端 未结 2 1354
深忆病人
深忆病人 2020-12-28 17:27

I am trying to create a table in sqlite that takes data from a csv file and adds an autoincrementing primary key to the first column. Here is the table I am trying to insert

相关标签:
2条回答
  • 2020-12-28 17:30

    You can add a column with the primary keys in your text file, then import it. Note: If you get that error on the first column, make sure your file does not have the BOM. If it has the BOM (3 bytes at the beginning of the file), it will fail on the first row.

    1: INSERT failed: datatype mismatch

    >     drop table words ;
    >     CREATE TABLE words (id INTEGER PRIMARY KEY AUTOINCREMENT, name COLLATE NOCASE, rank INT);
    >     .import file.txt words
    
    1|a|0
    2|b|0
    3|c|0
    4|d|0
    5|e|0
    6|f|0
    7|g|0
    
    0 讨论(0)
  • 2020-12-28 17:38

    An empty field in a CSV file is just an empty string, which is not valid for an INTEGER PRIMARY KEY column.

    Import into a temporary table without that column, then copy the data over with:

    INSERT INTO Allegiance(CharacterID, Title) SELECT * FROM TempTable;
    
    0 讨论(0)
提交回复
热议问题