我正在处理一个项目,我需要创建一个新表,然后从 CSV 导入数据。我在 StackOverflow 上阅读了许多类似的问题(“最后预期列之后的额外数据”)和答案,但我仍然没有找到罪魁祸首。
CREATE TABLE colleges2014_15 (
unitid integer,
intsnm text,
city text,
stabbr text,
zip_clean char,
control integer,
latitude float,
longitude float,
tutionfee_in float,
tuitionfee_out float,
pctpell float,
inc_pct_lo float,
dep_stat_pct_ind float,
dep_debt_mdn float,
ind_debt_mdn float,
pell_debt_mdn float,
ugds_men float,
ubds_women float,
locale integer,
PRIMARY KEY(unitid)
);
已成功创建包含 19 个不同列的表。然后我去尝试将数据导入到新表中。
COPY colleges2014_15(
unitid,
intsnm,
city,
stabbr,
zip_clean,
control,
latitude,
longitude,
tutionfee_in,
tuitionfee_out,
pctpell,
inc_pct_lo,
dep_stat_pct_ind,
dep_debt_mdn,
ind_debt_mdn,
pell_debt_mdn,
ugds_men,
ubds_women,
locale
)
FROM '/Users/compose/Downloads/CollegeScorecard_Raw_Data x/MERGED2014_15_cleaned.csv' CSV HEADER
;
然后我收到错误消息。我在 CSV 中完成了以下操作:
- 确保将其保存为 UTF-8 CSV(在 Mac 上工作)
- 已经清除了每一行中的所有逗号
- 清除所有 NULL 值
- 确认所有数据类型(整数、 float 、文本等)都正确
- 我尝试只复制第一列
unitid
;它失败了。我试过仅导入第二列 (intsnm
),但由于同样的错误而失败。
尝试复制所有 19 列时的完整错误消息如下:
An error occurred when executing the SQL command: COPY colleges2014_15( unitid, intsnm, city, stabbr, zip_clean, control, latitude, longitude, tutionfee_in, tuitionfee_out, pctpell, inc_pct_...
ERROR: extra data after last expected column Where: COPY colleges2014_15, line 2: "100654,Alabama A & M University,Normal,AL,35762,35762,1,34.783368,-86.568502,9096,16596,0.7356,0.651..." 1 statement failed.
Execution time: 0.03s
尝试仅复制第一列时的完整错误消息是:
An error occurred when executing the SQL command: COPY colleges2014_15( unitid ) FROM '/Users/compose/Downloads/CollegeScorecard_Raw_Data x/MERGED2014_15_cleaned.csv' CSV HEADER
ERROR: extra data after last expected column Where: COPY colleges2014_15, line 2: "100654,Alabama A & M University,Normal,AL,35762,35762,1,34.783368,-86.568502,9096,16596,0.7356,0.651..." 1 statement failed.
Execution time: 0.01s
非常感谢任何帮助。
最佳答案
我花了一段时间才弄清楚搜索错误的原因,所以发布了我的问题以帮助其他人。我的问题是对 pgAdmin 缺乏经验 pgAdmin 要求在导入数据之前使用列创建表。我原以为标题会从 .csv 文件中使用,我使用过的大多数其他包都是这样工作的。
如果您使用的是使用 PostGIS 的 GIS 系统,则有一个简单的解决方案。我使用的是 QGIS 3.4,安装了 Postgres 和 PostGIS。
在QGIS中
选择数据库菜单选项
选择数据库管理器
在左边 - 选择 table 的位置
选择导入图层/文件
在下一个窗口中选择以下
输入 - 选择文件
表 - 输入表名
好的
关于postgresql - 错误 : extra data after last expected column in postgres table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42968667/