我正在尝试将一些数据导入我的数据库.所以我创建了一个临时表,
create temporary table tmp(pc varchar(10), lat decimal(18,12), lon decimal(18,12), city varchar(100), prov varchar(2));
Run Code Online (Sandbox Code Playgroud)
而现在我正在尝试导入数据,
copy tmp from '/home/mark/Desktop/Canada.csv' delimiter ',' csv
Run Code Online (Sandbox Code Playgroud)
但后来我得到了错误,
ERROR: invalid byte sequence for encoding "UTF8": 0xc92c
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?我是否需要更改整个数据库的编码(如果是,如何?)或者我可以只更改tmp表格的编码吗?或者我应该尝试更改文件的编码?
我正在尝试使用psycopg2的copy_from()方法将数据从python的StringIO对象加载到Postgres数据库表中.
我的copy_from在第一个记录本身失败,特别是对于具有空值(''没有引号)的特定(可空)整数列.我也尝试使用Python的None关键字而不是''用于NULL值.它抛出以下错误: DataError:整数的输入语法无效:""语境:COPY,第1行,列:""
代码看起来像这样:
table_data = StringIO.StringIO()
# Populate the table_data variable with rows delimited by \n and columns delimited by \t
cursor = db_connection.cursor()
cursor.copy_from(table_data, <table_name>)
Run Code Online (Sandbox Code Playgroud)
此列是smallint列.