2016-08-02 3 views
1

, 나는이 SQL 요청을 실행하기 cr.execute을 사용하고 cr.execute (SQL)를OpenERP/Odoo - 문자열 내부의 견적이 작동하지 않습니다 - OpenERP 7에서

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 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

Perfect! 고맙습니다! – Selverine

관련 문제