How to import csv file to sqlite with correct data types

后端 未结 5 1281
再見小時候
再見小時候 2020-12-24 15:13

When I import a csv file to sqlite database, it imports number as string to integer column, how can I fix this? A line from my csv file like this:

 31,c,BB R         


        
5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-24 15:53

    You just need to create the table first with correct types and then the CSV-import will keep this types, because the table already exists.

    Here a sample:

    create table table1(name TEXT, wert INT);
    .mode csv
    .separator ";"
    .import "d:/temp/test.csv" table1
    

    If you need to delete an imported header-line then use something like this after the import:

    delete from table1 where rowid=1;
    

    or use this in case you already did multiple imports into the same table:

    delete from [table1] where "name"='name'; -- try to use a name of an INT-column for this.
    

    at the end you can just check the correct import like this:

    .header ON
    select * from table1 order by wert;
    

提交回复
热议问题