【问题标题】:Error with csv file while importing it into sqlite3将csv文件导入sqlite3时出错
【发布时间】:2021-03-12 19:31:25
【问题描述】:

所以我尝试使用 sqlite3 将 csv 文件导入到表中,但出现错误,这是表架构和我用来导入文件的命令以及它按顺序显示的错误:

CREATE TABLE Artist(
Art_ID int not null primary key,
Art_Name varchar(30),
Followers int,
Art_Genres varchar(200),
NumAlbums int,
YearFirstAlbum int,
Gender char(1),
Group_Solo varchar(5)
);
sqlite> 
sqlite> .import '| tail -n +2 /Users/adrianogiunta/Desktop/artistDF.csv' Artist
<pipe>:1: expected 8 columns but found 1 - filling the rest with NULL
<pipe>:2: expected 8 columns but found 1 - filling the rest with NULL
<pipe>:3: expected 8 columns but found 1 - filling the rest with NULL
<pipe>:4: expected 8 columns but found 1 - filling the rest with NULL
<pipe>:5: expected 8 columns but found 1 - filling the rest with NULL
<pipe>:6: expected 8 columns but found 1 - filling the rest with NULL

剩下的 1035 行。

这些是 csv 文件的第一行:

X,Artist,Followers,Genres,NumAlbums,YearFirstAlbum,Gender,Group.Solo
0,Ed Sheeran,52698756,"pop,uk pop",8,2011,M,Solo
1,Justin Bieber,30711450,"canadian pop,dance pop,pop,post-teen pop",10,2009,M,Solo
2,Jonas Brothers,3069527,"boy band,dance pop,pop,post-teen pop",10,2006,M,Group
3,Drake,41420478,"canadian hip hop,canadian pop,hip hop,pop rap,rap,toronto rap",11,2010,M,Solo
4,Chris Brown,9676862,"dance pop,pop,pop rap,r&b,rap",6,2005,M,Solo
5,Taylor Swift,23709128,"dance pop,pop,post-teen pop",10,2006,F,Solo

这是我的表格之后显示的内容:

sqlite> SELECT * FROM Artist LIMIT 5;
0,Ed Sheeran,52698756,"pop,uk pop",8,2011,M,Solo|||||||
1,Justin Bieber,30711450,"canadian pop,dance pop,pop,post-teen pop",10,2009,M,Solo|||||||
2,Jonas Brothers,3069527,"boy band,dance pop,pop,post-teen pop",10,2006,M,Group|||||||
3,Drake,41420478,"canadian hip hop,canadian pop,hip hop,pop rap,rap,toronto rap",11,2010,M,Solo|||||||
4,Chris Brown,9676862,"dance pop,pop,pop rap,r&b,rap",6,2005,M,Solo|||||||
sqlite> 

提前感谢您的帮助!

【问题讨论】:

  • @AdrianoGiunta 上面写着expected 8 columns but found 1 - filling the rest with NULL,那么实际放在桌子上的是什么?
  • 编辑我的问题来回答你的问题!
  • 您似乎忘记了“.separator”设置。您需要告诉 SQLite3 字段用逗号而不是默认分隔符分隔,否则它将看不到分隔符,并且 - 正如您发现的那样 - 相信每一行都是一个字段。

标签: sqlite csv


【解决方案1】:

.import 命令默认查找 SQLite 的转储格式。在尝试导入 CSV 之前,您需要输入 .mode csv。正如文档所述。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-16
    • 1970-01-01
    • 1970-01-01
    • 2015-02-06
    • 2016-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多