如何通过python将current_timestamp插入Postgres

发布于 2024-11-08 05:15:31 字数 160 浏览 0 评论 0原文

我需要将行插入 PG,其中一个字段是带有时间戳的日期和时间,这是事件发生的时间,所以我不能使用 -->;插入时 Postgres 的 current_timestamp 函数,那么我如何将之前收集的时间和日期插入到 pg 行中,其格式与 current_timestamp 在该时间点创建的格式相同。

I need to insert rows into PG one of the fields is date and time with time stamp, this is the time of incident, so I can not use --> current_timestamp function of Postgres at the time of insertion, so how can I then insert the time and date which I collected before into pg row in the same format as it would have been created by current_timestamp at that point in time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(8

淡水深流 2024-11-15 05:15:31

如果您使用 psycopg2 (可能还有其他一些客户端库) ,你可以简单地传递一个Python datetime 对象作为 SQL 的参数 -query:(

from datetime import datetime, timezone

dt = datetime.now(timezone.utc)
cur.execute('INSERT INTO mytable (mycol) VALUES (%s)', (dt,))

假设数据库端使用timestamp with time zone类型。)

更多可以适应SQL的Python类型(并在执行查询时作为Python对象返回)是此处列出

If you use psycopg2 (and possibly some other client library), you can simply pass a Python datetime object as a parameter to a SQL-query:

from datetime import datetime, timezone

dt = datetime.now(timezone.utc)
cur.execute('INSERT INTO mytable (mycol) VALUES (%s)', (dt,))

(This assumes that the timestamp with time zone type is used on the database side.)

More Python types that can be adapted into SQL (and returned as Python objects when a query is executed) are listed here.

⊕婉儿 2024-11-15 05:15:31

时间戳没有“格式”。

处理时间戳的推荐方法是使用PreparedStatement,您只需在SQL 中传递一个占位符,并通过编程语言的API 传递一个“真实”对象。由于我不了解Python,所以我不知道它是否支持PreparedStatements 以及其语法如何。

如果要将时间戳文字放入生成的 SQL 中,则在指定值时需要遵循一些格式规则(文字确实有格式)。

Ivan 的方法会起作用,尽管我不能 100% 确定它是否取决于 PostgreSQL 服务器的配置。

指定时间戳文字的独立于配置(和语言)的解决方案是 ANSI SQL 标准:

 INSERT INTO some_table 
 (ts_column) 
 VALUES 
 (TIMESTAMP '2011-05-16 15:36:38');

是的,那就是关键字 TIMESTAMP 后跟以 ISO 样式格式化的时间戳(TIMESTAMP关键字定义该格式)

另一个解决方案是使用 to_timestamp() 函数,您可以在其中指定输入文字的格式。

 INSERT INTO some_table 
 (ts_column) 
 VALUES 
 (to_timestamp('16-05-2011 15:36:38', 'dd-mm-yyyy hh24:mi:ss'));

A timestamp does not have "a format".

The recommended way to deal with timestamps is to use a PreparedStatement where you just pass a placeholder in the SQL and pass a "real" object through the API of your programming language. As I don't know Python, I don't know if it supports PreparedStatements and how the syntax for that would be.

If you want to put a timestamp literal into your generated SQL, you will need to follow some formatting rules when specifying the value (a literal does have a format).

Ivan's method will work, although I'm not 100% sure if it depends on the configuration of the PostgreSQL server.

A configuration (and language) independent solution to specify a timestamp literal is the ANSI SQL standard:

 INSERT INTO some_table 
 (ts_column) 
 VALUES 
 (TIMESTAMP '2011-05-16 15:36:38');

Yes, that's the keyword TIMESTAMP followed by a timestamp formatted in ISO style (the TIMESTAMP keyword defines that format)

The other solution would be to use the to_timestamp() function where you can specify the format of the input literal.

 INSERT INTO some_table 
 (ts_column) 
 VALUES 
 (to_timestamp('16-05-2011 15:36:38', 'dd-mm-yyyy hh24:mi:ss'));
梦初启 2024-11-15 05:15:31

几乎任何合理的格式都接受日期和时间输入,
包括 ISO 8601、SQL 兼容、传统 POSTGRES 等。
对于某些格式,日期输入中的月、日和年的顺序是
不明确,并且支持指定预期的顺序
这些字段。

换句话说:只要写任何东西,它就会起作用。

或者检查此表所有明确的格式。

Date and time input is accepted in almost any reasonable format,
including ISO 8601, SQL-compatible, traditional POSTGRES, and others.
For some formats, ordering of month, day, and year in date input is
ambiguous and there is support for specifying the expected ordering of
these fields.

In other words: just write anything and it will work.

Or check this table with all the unambiguous formats.

落花浅忆 2024-11-15 05:15:31

当然,只需以以下格式为该时间戳列传递一个字符串值:'2011-05-16 15:36:38'(您也可以在其中附加时区,例如 'PST' )。 PostgreSQL 会自动将字符串转换为时间戳。请参阅http://www.postgresql.org/ docs/9.0/static/datatype-datetime.html#DATATYPE-DATETIME-INPUT

Sure, just pass a string value for that timestamp column in the format: '2011-05-16 15:36:38' (you can also append a timezone there, like 'PST'). PostgreSQL will automatically convert the string to a timestamp. See http://www.postgresql.org/docs/9.0/static/datatype-datetime.html#DATATYPE-DATETIME-INPUT

半夏半凉 2024-11-15 05:15:31
from datetime import datetime as dt

然后在您的代码中使用它:

cur.execute('INSERT INTO my_table (dt_col) VALUES (%s)', (dt.now(),))
from datetime import datetime as dt

then use this in your code:

cur.execute('INSERT INTO my_table (dt_col) VALUES (%s)', (dt.now(),))
九八野马 2024-11-15 05:15:31

使用 strftime 实现 datetime 目标:

x.strftime(r'%Y-%m-%d %H:%M:%S')

这里的主要思想是将 datetime 转换为 Postgres 没有时区的时间戳,插入时应该是一个字符串。

Use strftime for datetime objectives:

x.strftime(r'%Y-%m-%d %H:%M:%S')

The main idea here to transform the the datetime into a Postgres timestamp without timezone, which should be a string when insert.

知你几分 2024-11-15 05:15:31

只需使用

现在()

CURRENT_TIMESTAMP

我更喜欢后者,因为我喜欢没有额外的括号,但这只是个人喜好。

Just use

now()

or

CURRENT_TIMESTAMP

I prefer the latter as I like not having additional parenthesis but thats just personal preference.

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