NameError:未定义名称“截止日期”

2024-10-01 04:44:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一个python脚本,以便在PostgreSQL 11.0表中插入值。该列的日期信息格式为MonthYear(如2016年5月)

我在代码中使用以下行插入值

q2 = """insert into %s.test_trial2enrollment_1 (trial_id, enrollment, enrollment_type, primary_completion_date, primary_completion_type, study_completion_date, study_completion_type, study_start, last_version_checked)
        values ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s');""" % (staging_schema, id_ ,enr_val, enr_type, to_date(prm_val, 'MonYYYY'), prm_type, to_date(end_val, 'MonYYYY'), end_type, sta, str(clinical_trial_version - 1))
        cur.execute(q2)

我得到以下错误

NameError: name 'to_date' is not defined

如何使用以下格式插入日期值

May2016
Nov2018
Mar2020

非常感谢您的帮助


Tags: toiddateversiontypevalcompletiontrial
1条回答
网友
1楼 · 发布于 2024-10-01 04:44:37

为此:

q2 = """insert into %s.test_trial2enrollment_1 (trial_id, enrollment, enrollment_type, primary_completion_date, primary_completion_type, study_completion_date, study_completion_type, study_start, last_version_checked)
        values (%s, %s, %s, %s, to_date(%s, 'MonYYYY'), %s, to_date(%s, 'MonYYYY'), %s, %s)"""
        cur.execute(q2, [staging_schema, id_ ,enr_val, enr_type, prm_val, prm_type, end_val, end_type, sta, str(clinical_trial_version - 1)])

此处的每个文档:

向SQL查询传递参数 https://www.psycopg.org/docs/usage.html#passing-parameters-to-sql-queries

相关问题 更多 >