Невозможно импортировать более одной строки из CSV-файла в базу данных sqlite. - PullRequest
2 голосов
/ 02 июня 2011

У меня проблемы с попыткой импортировать весь 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"""

Ответы [ 2 ]

1 голос
/ 28 октября 2011

Команда sqlite .import понимает только код конца строки LF (0xA), а не CR (0xD). Проверьте ваш входной файл в hex-редакторе.

0 голосов
/ 02 июня 2011

Вам нужно поместить кавычки вокруг полей с пробелами.

Как это

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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...