2016-08-02 12 views
1

在OpenERP的7,我使用的是cr.execute執行SQL請求的OpenERP/Odoo - 在字符串中報價不工作 - cr.execute(SQL)

cr.execute('select distinct(value) from ir_translation where name = \'product.bat3,name\' and src = \''+ str(res_bat[j][0].encode('utf-8'))+'\' and res_id = '+ str(res_bat[j][1])+' and lang = \''+ str(line2.partner_id.lang)+'\'') 

然而,我的字符串res_bat [ j] [0]是一個帶有 引號的字符串。該字符串是:測試的 因此我有波紋管的錯誤:

ProgrammingError: syntax error at or near "s" 
LINE 1: ... where name = 'product.bat3,name' and src = 'test's' and res... 

如何修改我的SQL請求,糾正這一錯誤?

回答

1

您不得在SQL查詢中自己執行替換,因爲這會使您的代碼容易受到SQL injections的影響。

正確的版本是:

cr.execute(
    'select distinct(value) from ir_translation ' 
    'where name = %s and src = %s and res_id = %S and lang = %s', 
    ('product.bat3,name', 
    res_bat[j][0].encode('utf-8'), 
    res_bat[j][1], 
    line2.partner_id.lang) 
) 

你可以保留的第一個參數的查詢,如果你的願望。

+0

完美!謝謝! – Selverine