我在Postgres中有一个表,列名为TIME(UpperCase)。 使用SQL命令从Postgres本身将csv插入此表很容易。
COPY american_district FROM 'O:\Python\PostGREsql\district.csv' WITH CSV HEADER DELIMITER AS ',' NULL AS '\N';
但是使用下面的python代码将相同的csv插入到表中会给出错误
f = open('O:\Python\PostGREsql\district.csv')
cur_DBKPI.copy_from(f, 'american_district', sep=',', null='')
ERROR:
psycopg2.DataError: invalid input syntax for type date: "TIME"
CONTEXT: COPY american_district, line 1, column time: "TIME"
我发现最佳做法是将列保持在小写但是有任何解决方法吗?
答案 0 :(得分:0)
通过更改我的查询的语法来使用" copy_expert"来添加标题。
f = open('O:\Python\PostGREsql\district.csv')
sql = "COPY american_district FROM STDIN WITH CSV HEADER DELIMITER AS ',' NULL AS '\\N'"
cur_DBKPI.copy_expert(sql=sql,file=f)