我必须使用哪种 Postgres
日期类型来遵循日期格式?
Sun, 17 Dec 2017 14:26:07 GMT
我正在使用 TIMESTAMPTZ
并收到此错误:
Error: invalid input syntax for type timestamp with time zone: "p"
LINE 2: VALUES ('h', 't', 't', ('p'), ('s'))
^
我应该在插入之前转换这样的日期吗?
最佳答案
您可能正在尝试将日期时间作为字符串插入。使用 datetime
对象和 let the database driver handle the type conversion automatically 进行参数化:
from datetime import datetime
date_string = "Sun, 17 Dec 2017 14:26:07 GMT"
dt = datetime.strptime(date_string, "%a, %d %b %Y %H:%M:%S %Z")
cursor.execute('INSERT INTO some_table (somecol) VALUES (%s)', (dt, ))
关于python-3.x - 为 "Sun, 17 Dec 2017 14:26:07 GMT"设置日期格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47862098/