【发布时间】:2021-12-13 13:53:57
【问题描述】:
我有一个名为 Data2 的数据框,我希望将它的值放入 postgresql 表中。由于某些原因,我不能使用 to_sql,因为 Data2 中的某些值是 numpy 数组。
这是 Data2 的架构:
cursor.execute(
"""
DROP TABLE IF EXISTS Data2;
CREATE TABLE Data2 (
time timestamp without time zone,
u bytea,
v bytea,
w bytea,
spd bytea,
dir bytea,
temp bytea
);
"""
)
我的代码段:
for col in Data2_mcw.columns:
for row in Data2_mcw.index:
value = Data2_mcw[col].loc[row]
if type(value).__module__ == np.__name__:
value = pickle.dumps(value)
cursor.execute(
"""
INSERT INTO Data2_mcw(%s)
VALUES (%s)
"""
,
(col.replace('\"',''),value)
)
产生错误:
psycopg2.errors.SyntaxError: syntax error at or near "'time'"
LINE 2: INSERT INTO Data2_mcw('time')
如何纠正这个错误?
任何帮助将不胜感激!
【问题讨论】:
标签: python sql postgresql dataframe