如何通过python将current_timestamp插入Postgres
我需要将行插入 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
如果您使用
psycopg2
(可能还有其他一些客户端库) ,你可以简单地传递一个Pythondatetime
对象作为 SQL 的参数 -query:(假设数据库端使用
timestamp with time zone
类型。)更多可以适应SQL的Python类型(并在执行查询时作为Python对象返回)是此处列出。
If you use
psycopg2
(and possibly some other client library), you can simply pass a Pythondatetime
object as a parameter to a SQL-query:(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.
时间戳没有“格式”。
处理时间戳的推荐方法是使用PreparedStatement,您只需在SQL 中传递一个占位符,并通过编程语言的API 传递一个“真实”对象。由于我不了解Python,所以我不知道它是否支持PreparedStatements 以及其语法如何。
如果要将时间戳文字放入生成的 SQL 中,则在指定值时需要遵循一些格式规则(文字确实有格式)。
Ivan 的方法会起作用,尽管我不能 100% 确定它是否取决于 PostgreSQL 服务器的配置。
指定时间戳文字的独立于配置(和语言)的解决方案是 ANSI SQL 标准:
是的,那就是关键字
TIMESTAMP
后跟以 ISO 样式格式化的时间戳(TIMESTAMP
关键字定义该格式)另一个解决方案是使用 to_timestamp() 函数,您可以在其中指定输入文字的格式。
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:
Yes, that's the keyword
TIMESTAMP
followed by a timestamp formatted in ISO style (theTIMESTAMP
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.只需使用“现在”
http://www.postgresql.org/docs/ 8.0/static/datatype-datetime.html
Just use 'now'
http://www.postgresql.org/docs/8.0/static/datatype-datetime.html
换句话说:只要写任何东西,它就会起作用。
或者检查此表所有明确的格式。
In other words: just write anything and it will work.
Or check this table with all the unambiguous formats.
当然,只需以以下格式为该时间戳列传递一个字符串值:
'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然后在您的代码中使用它:
then use this in your code:
使用
strftime
实现datetime
目标:这里的主要思想是将
datetime
转换为 Postgres没有时区的时间戳
,插入时应该是一个字符串。Use
strftime
fordatetime
objectives:The main idea here to transform the the
datetime
into a Postgrestimestamp without timezone
, which should be a string when insert.只需使用
或
我更喜欢后者,因为我喜欢没有额外的括号,但这只是个人喜好。
Just use
or
I prefer the latter as I like not having additional parenthesis but thats just personal preference.