2016-08-02 8 views
1

をOpenERP 7では、私は[ただし、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をcr.executeを使用しています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 injectionsに脆弱になるため、SQLクエリで置換を実行してはいけません。

正しいバージョンは次のとおりです。

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

関連する問題