postgresql+python:如何适应co​​py_from?

发布于 2024-12-13 17:35:19 字数 247 浏览 4 评论 0原文

我可以使用什么函数来调整值以适合在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

女中豪杰 2024-12-20 17:35:19

您不应该只将 adaptcopy_from 一起使用。

copy_from 需要与 SQL 引用不同的格式。

对于字符串,我建议编写您自己的 copy_adapt 函数,该函数应使用 \t 转义制表符,使用 \r 转义 CR,使用 \r 转义 LF <代码>\n。

时间戳/日期列的值的格式应类似于您在 Postgres 中 SELECT now() 时看到的字符串。

元组元素应该用制表符分隔,整个元组用换行符结束。

You shouldn't just use adapt with copy_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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文