[英]update a postgres table column value in python?
I have one offset date to be stored in Postgres table -我有一个偏移日期要存储在 Postgres 表中 -
latest_mod_date = max(data['last_modified'])
latest mod_date = 2022-04-16 05:50:00
Want this date to be updated in postgres -希望在 postgres 中更新此日期 -
Used this query - The postgres table data_offset is like this -使用此查询 - postgres 表 data_offset 是这样的 -
_id offset_type offset_value
1 C [NULL]
2 P [NULL]
# # query to update - global is the schema and data_offset is the data table in postgres
sql = '''update global.data_offset set {offset_value=latest_mod_date} where {offset_type='C'}; '''
#
# # execute the query
cursor.execute(sql)
print("Table updated..")
print('Table after updation...')
# query to display table
sql2 = 'select * from global.data_offset;'
# execute query
cursor.execute(sql2);
# fetching all details
print(cursor.fetchall());
This query is giving error -此查询给出错误 -
psycopg2.errors.SyntaxError: syntax error at or near "{"
LINE 1: update global.data_offset set {offset_value=latest_mo...
I want the latest_mod_date to be stored in the offset value where the offset_stypr is 'C'.我希望将 latest_mod_date 存储在 offset_stypr 为“C”的偏移值中。
Any place you have seem {}
is probably because people have been doing something like sql_str.format()
which is the wrong thing to do.你看到的任何地方{}
可能是因为人们一直在做类似sql_str.format()
的事情,这是错误的做法。 See Parameter passing for the correct way to pass variables in.有关传递变量的正确方法,请参阅参数传递。
In your case what you need is:在您的情况下,您需要的是:
sql = "update global.data_offset set offset_value = %s where offset_type='C'"
cursor.execute(sql, [latest_mod_date])
# Commit the changes.
con.commit()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.