首页 > 解决方案 > 无法将时间戳插入 Postgresql psycopg2

问题描述

我正在尝试将时间戳作为我的 Postresql 查询的列中的值,但我很想以各种不同的方式得到各种格式错误,我已经尝试过了。

我有 3 个作为时间戳的列:time_stamp、start_date、end_date

start_date = datetime.strptime('2020-03-17 16:00:00', "%Y-%m-%d %H:%M:%S").timestamp()
end_date = datetime.strptime('2020-03-17 16:00:00', "%Y-%m-%d %H:%M:%S").timestamp()

sql_insert_query = 'INSERT INTO public.table(time_stamp, name, name_short, geo, link, img_url, source, type, category, start_date, end_date, quantity, remaining)' \
              ' VALUES ( current_timestamp, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s,%s, %s);'

insert_tuple = (item['name'],'name_short',item['language'],item['_url'],item['image_url'],'_API',item['coupon_type'],item['category'],start_date ,end_date,coupon_limits,item['unused'])

cur = conn.cursor()
cur.execute(sql_insert_query, insert_tuple)

错误是(不准确的翻译):

psycopg2.ProgrammingError: the column «start_date» is of type timestamp without time zone but the expression is type numeric

在 SQL 表上,我已将列声明为 time_stamp timestamp without time zone NOT NULL DEFAULT now(), start_date timestamp without time zone, end_date timestamp without time zone NOT NULL,

标签: pythonpostgresql

解决方案


推荐阅读