我正在尝试在表中执行大容量插入
CREATE TABLE IF NOT EXISTS test (symbol VARCHAR(16), ts timestamp)
我有一个要插入表单的值数组
^{pr2}$我的插入查询如下所示
insert into test VALUES %s
执行插入的python代码如下所示
psycopg2.extras.execute_values(cursor, insert_stmt, [('AAPL',1528004700), ('AAPL', 1528005600)])
我有个错误
ProgrammingError: column "ts" is of type timestamp without time zone but expression is of type integer
LINE 1: insert into test VALUES ('AAPL',1528004700),('AAPL',15280056...
^
HINT: You will need to rewrite or cast the expression.
我知道to_timestamp可以在插入时解决这个问题,但是execute_值不允许我添加多个占位符,我需要每次执行大容量插入。时间戳应该没有时区。我如何修正这个错误
谢谢!在
更新1
execute_batch()可以完美地工作,因为我可以在占位符部分添加到\u timestamp
insert = "insert into test VALUES (%s, to_timestamp(%s))"
其次是
psycopg2.extras.execute_batch(cur, insert, [('AAPL',1528004700), ('AAPL', 1528005600)])
不过,我希望使用execute\u值,因为它比execute\u batch()稍快一些
我不确定
psycopg2.extras.execute_values
,但通常的方法是使用executemany
方法插入一个记录列表insert语句是用
%s
为每一列编写的。在这种特殊情况下,我们还需要将整数转换为时间戳。幸运的是,postgresql提供了一个tou timestamp函数来实现这一点。在这应该是有效的:
编辑: 一般来说,Postgres无法猜测您的1528004700是一个时间戳,您需要以某种方式显式地声明它。使用to_timestamp的解决方案将“this is a timestamp”放在Postgres端,上面的代码将其放在python端。从信息角度看它们是等价的,我没有检查哪一个更有效。在
相关问题 更多 >
编程相关推荐