背景信息
我需要使用 csv 文件作为输入从命令行创建一个 sqlite 数据库(因为所有这些最终都会在 bash 脚本中结束)。
问题
当我尝试运行 .import 命令时,收到错误:预期 9 列,但发现 1 - 用 NULL 填充其余部分
**代码/命令**
我正在尝试运行以下命令:
mydevbox:/tmp/# sqlite3 widgets.db ".import /tmp/widgets_2017-03-15-14-36.csv users"
/tmp/widgets_2017-03-15-14-36.csv:1: expected 9 columns but found 1 - filling the rest with NULL
/tmp/widgets_2017-03-15-14-36.csv:2: expected 9 columns but found 1 - filling the rest with NULL
/tmp/widgets_2017-03-15-14-36.csv:3: expected 9 columns but found 1 - filling the rest with NULL
/tmp/widgets_2017-03-15-14-36.csv:4: expected 9 columns but found 1 - filling the rest with NULL
/tmp/widgets_2017-03-15-14-36.csv:5: expected 9 columns but found 1 - filling the rest with NULL
/tmp/widgets_2017-03-15-14-36.csv:6: expected 9 columns but found 1 - filling the rest with NULL
用户表没有主键。架构如下所示:
mydevbox:/tmp# sqlite3 widgets.db
SQLite version 3.8.10.2 2015-05-20 18:17:19
Enter ".help" for usage hints.
sqlite> .schema
CREATE TABLE users(fname varchar(100), lname varchar(100), description varchar(100),grp INTEGER, group_name varchar(100), destination_id varchar(100), w_number varchar(10), destination varchar(100), class varchar(10));
sqlite>
我认为这可能是因为我没有指定分隔符。所以我尝试了这个:
mydevbox:/tmp# sqlite3 widget.db ".separator ,; .import /tmp/widgets_2017-03-15-14-36.csv users"
Usage: .separator COL ?ROW?
如您所见,它因有关分隔符的错误而失败。
以下是 csv 的前几行:
Jane,Doe,asdf,0,"",1,1234,4444,sip
John,Doe,,0,,2,12752,<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a7cdc3c8c2e7dec6cfc8c889c4c8ca" rel="noreferrer noopener nofollow">[email protected]</a>,smtp
John,Doe,,0,,2,12752,<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="89e3ede6ecbbc9f0e8e1e6e6a7eae6e4" rel="noreferrer noopener nofollow">[email protected]</a>,smtp
JJ,Blaird,,0,,4,12477,<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="30525c5159425470575d51595c1e535f5d" rel="noreferrer noopener nofollow">[email protected]</a>,smtp
如有任何建议,我们将不胜感激。 FWIW,此 csv 是通过针对 postgresql 数据库的 select 语句创建的。
最佳答案
只需使用命令行标志即可:
sqlite3 widget.db -separator ',' '.import /tmp/widgets_2017-03-15-14-36.csv users'
这对我有用。
关于bash - 尝试将 csv 导入 sqlite 数据库失败并出现错误 - 预期 9 列,但发现 1 - 用 NULL 填充其余部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42813252/