PGSQL查询中的参数传递

时间:2016-06-14 09:22:35

标签: python postgresql openerp openerp-7

我试图在我的类中调用一个需要携带sql语句的create函数。我需要在该查询中传递一些参数,但我不知道该怎么做。

roster_substitute类中的create函数

def create(self, cr, uid, values, context=None):
        rec_id=values['id']
        sub_day=values['roster_day']
        ros_time=values['time_slot']

   cr.execute("""UPDATE roster_days_allocation  SET roster_allocation_connection = (SELECT MAX(ra.id) FROM roster_allocation ra, roster_substitution rs
                            WHERE ra.emp_id=rs.sub_employee) 
                      WHERE allocation_start_day = **'sub_day'** AND roster_time_list = **ros_time**""")

return super(roster_substitution, self).create(cr, uid, values, context=context)

1 个答案:

答案 0 :(得分:1)

如果您正在使用标准的psycopg2库,那么

cr.execute("""UPDATE roster_days_allocation SET roster_allocation_connection = (SELECT MAX(ra.id) FROM roster_allocation ra, roster_substitution rs
                            WHERE ra.emp_id=rs.sub_employee) 
              WHERE allocation_start_day = %s AND roster_time_list = %s""", (sub_day, ros_time))

请参阅psycopg2 documentation