postgresql+python:如何适应copy_from?
我可以使用什么函数来调整值以适合在 COPY FROM 语句中使用?我尝试了这个 adapt
函数:
from psycopg2.extensions import adapt
但是,它给出了错误的日期时间(将 ::timestsamp
附加到字符串,postgres 不喜欢它)和字符串(包装它们)用单引号括起来,例如空字符串是 '',似乎不应该使用引号)。
What function can I use to adapt values for suitable use in a COPY FROM
statement? I tried this adapt
function:
from psycopg2.extensions import adapt
However, it gives the wrong thing for datetimes (appends ::timestsamp
to the string, postgres doesn't like it) and string (wraps them in single quotes, e.g. empty string is '', where it seems no quotes should be used).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不应该只将
adapt
与copy_from
一起使用。copy_from
需要与 SQL 引用不同的格式。对于字符串,我建议编写您自己的
copy_adapt
函数,该函数应使用\t
转义制表符,使用\r
转义 CR,使用\r
转义 LF <代码>\n。时间戳/日期列的值的格式应类似于您在 Postgres 中
SELECT now()
时看到的字符串。元组元素应该用制表符分隔,整个元组用换行符结束。
You shouldn't just use
adapt
withcopy_from
.copy_from
expects a format different than SQL quoting.For strings, I'd suggest to write your own
copy_adapt
function, which should escape tabs with\t
, CR with\r
and LF with\n
.Values for timestamp / date columns should be formatted (with strftime?) like the string which you see when you
SELECT now()
in Postgres.Tuple elements should be saparated by tabs, and whole tuple finished with newline.