OpenERP/Odoo - 字符串中的引号不起作用 - cr.execute(SQL)
OpenERP/Odoo - Quote inside a string is not working - cr.execute(SQL)
在 OpenERP 7 中,我使用 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] 是一个带有
引用。字符串是:test's
因此我有以下错误:
ProgrammingError: syntax error at or near "s"
LINE 1: ... where name = 'product.bat3,name' and src = 'test's' and res...
如何修改我的 SQL 请求以更正此错误?
您不得在 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)
)
如果您愿意,可以保留查询中的第一个参数。
在 OpenERP 7 中,我使用 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] 是一个带有 引用。字符串是:test's 因此我有以下错误:
ProgrammingError: syntax error at or near "s"
LINE 1: ... where name = 'product.bat3,name' and src = 'test's' and res...
如何修改我的 SQL 请求以更正此错误?
您不得在 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)
)
如果您愿意,可以保留查询中的第一个参数。