2011-06-02 155 views
1

我在尝试将整个csv文件导入到sqlite数据库时遇到问题。这些都是我的终端命令:无法从csv文件导入多个行到sqlite数据库

sqlite3 DB.sql 
create table table1 (id integer primary key, question VARCHAR(500), Aanswer VARCHAR(255), Banswer VARCHAR(255), Canswer VARCHAR(255), Danswer VARCHAR(255)); 
.mode csv 
.import db.csv table1 
select * from table1 

和输出:

7,"Who's head did Courtney Cox say was on her body in Scream 2?","Heather Graham",Oprah,"*Jennifer Aniston","Demi Moore" 

你会发现,它只是把周围的几个领域,这似乎是随机的引号......我不不知道这是为什么它不会继续导入下一行。

谢谢!

编辑:

这里是我的csv文件的前几行:

1,What movie follows Cher and Dionne named after great singers of the past that now do infomercials?,10 Things I Hate About You,Cant Hardly Wait,*Clueless,Freeway 
2,What 90s movie did critic Janet Maslin describe as : A gale-force movie with the energy to blow audiences right out of the theater?,Avalanche,Aftershocks,Armageddon,*Twister 
3,What actress declined Neve Campbell's role in Scream?,*Drew Barrymore,Carla Hatley,Courteney Cox,Rose McGowan 

如果我把引号中的自己,它吐出来的东西是这样的:

"""What was the name of Milla Jovovich's character in the Fifth Element?""","""Billy""","""Fog""","""Mugger""","""*Leeloo""" 
+0

我的猜测是你的CSV文件有问题。请发布前10行。 – ravenspoint 2011-06-02 18:43:38

回答

0

您需要在带有空格的字段周围加引号。

像这样

C:\Documents and Settings\james\My Documents>type test.csv 
1,field_with_no_spaces,'field with spaces' 
2,field_with_no_spaces,'field with spaces' 
3,field_with_no_spaces,'field with spaces' 
4,field_with_no_spaces,'field with spaces' 

C:\Documents and Settings\james\My Documents>sqlite3 test.dat 
SQLite version 3.6.19 
Enter ".help" for instructions 
Enter SQL statements terminated with a ";" 
sqlite> create table t1 (id integer primary key, q1, q2); 
sqlite> .mode csv 
sqlite> .import test.csv t1 
sqlite> select * from t1; 
1,field_with_no_spaces,"'field with spaces'" 
2,field_with_no_spaces,"'field with spaces'" 
3,field_with_no_spaces,"'field with spaces'" 
4,field_with_no_spaces,"'field with spaces'" 

您将获得 '额外' 的字段引号用空格。不是问题。重要的是整个文件被导入,对吧?

如果你关心的额外quites,关闭CSV模式,这样

sqlite> .mode list 
sqlite> .separator , 
sqlite> select * from t1; 
1,field_with_no_spaces,'field with spaces' 
2,field_with_no_spaces,'field with spaces' 
3,field_with_no_spaces,'field with spaces' 
4,field_with_no_spaces,'field with spaces' 

把你的数据引号包围,给人

1,What movie follows Cher and Dionne named after great singers of the past that now do infomercials?,10 Things I Hate About You,Cant Hardly Wait,*Clueless,Freeway 
2,What 90s movie did critic Janet Maslin describe as : A gale-force movie with the energy to blow audiences right out of the theater?,Avalanche,Aftershocks,Armageddon,*Twister 
3,What actress declined Neve Campbell's role in Scream?,*Drew Barrymore,Carla Hatley,Courteney Cox,Rose McGowan 
+0

我仍然看不到您正在输入的csv文件。 – ravenspoint 2011-06-02 19:39:00

+0

我试过了,它看起来就像你展示的一样,但它仍然只会导入第一行。 – 2011-06-02 19:55:18

+0

请发布您尝试输入的数据。我可以看到的数据没有保护包含空格的字段的引号。 – ravenspoint 2011-06-02 20:07:30

1

源码的.IMPORT命令只能理解LF(是0xA )结束行代码,而不是CR(0xD)。用十六进制编辑器检查输入文件。