2017-03-01 116 views
0

如何在冲突时使用psycopg2查询参数? 例如,我有这样的数据:插入与查询参数冲突

#This is the data for the insert query 
q_values = {'customer_app_id': '35', 'email': '[email protected]', 'access_counter': 1, 'company_name': 'twitter', 'status': 'start'} 

#This is the data for the on conflict query part 
conflict_values = {'access_counter': +1, 'status': 'check', 'status_info': 'already exist'} 

这是查询:

insert into access (customer_app_id,email,access_counter,company_name,status) 
values (%s,%s,%s,%s,%s) ON CONFLICT (customer_app_id,email) DO UPDATE SET 
%s,%s,%s RETURNING * 

然后我跑这条线:

q_values = q_values.update(conflict_values) 
cursor.execute(query, q_values) 

首先,如何在所有冲突运行与查询参数?

其次,我对dict所做的更新并不好,因为如果它将是重复的键,它将合并它们,然后参数的数量将不会等于值的数量。

  • 和access_counter +1 - 冲突我试图通过1

增加访问_counter请你能帮忙吗? 谢谢!

回答

0

当你传递值的字典(而不是列表或元组)时,我认为你需要使用%(name)s占位符,而不是%s

这意味着您可以为占位符提供其他名称,以免它们在q_values字典中发生冲突。

有关详细信息,请参阅documentation