【问题标题】:Cannot import more than one line from csv file into sqlite database无法从 csv 文件导入多于一行到 sqlite 数据库
【发布时间】:2011-06-02 17:20:58
【问题描述】:

我在尝试将整个 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"""

【问题讨论】:

  • 我猜你的 CSV 文件有问题。请发布前 10 行。

标签: sqlite csv


【解决方案1】:

sqlite 的 .import 命令只能理解 LF (0xA) 行尾代码,而不是 CR (0xD)。在十六进制编辑器中检查您的输入文件。

【讨论】:

    【解决方案2】:

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

    这样

    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'"
    

    您将在带有空格的字段周围获得“额外”引号。不是问题。重要的是整个文件都被导入了,对吧?

    如果你关心额外的问题,请关闭 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
    

    【讨论】:

    • 我仍然看不到您尝试输入的 csv 文件。
    • 我试过了,它看起来就像你展示的那样,但它仍然只会导入第一行。
    • 请张贴您尝试输入的数据。我可以看到的数据没有引号保护包含空格的字段。
    • '评论家珍妮特·马斯林将 90 年代的电影描述为:一部能将观众直接从影院中吹走的狂风电影?',雪崩,余震,世界末日,*龙卷风
    • '哪个女演员拒绝了 Neve Campbell 在《呐喊》中的角色?','*德鲁·巴里摩尔','卡拉·哈特利','Courteney Cox','Rose McGowan'
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-21
    • 1970-01-01
    • 2012-05-04
    • 2016-08-14
    • 2014-08-18
    相关资源
    最近更新 更多