简体   繁体   English

在Python中使用“”执行SQL

[英]Executing SQL with “ ” from Python

I have trouble executing a SQL-statement from Python because of the " " in the selection. 由于选择中的“”,我无法从Python执行SQL语句。 I think I've tried every combination, but can't figure it out. 我想我已经尝试了每种组合,但无法弄清楚。 Can you please help me? 你能帮我么?

cnxn = pyodbc.connect("DRIVER={Microsoft Access Driver (*.mdb)};DBQ=c:\\python33\salesdb.mdb")
cursor = cnxn.cursor()
v_sql = "SELECT DISTINCT tblSeller.ID, tblSeller.Navn FROM tblResult INNER JOIN tblSeller ON tblResult.SellerID = tblSeller.ID WHERE (((tblSeller.Name)="Robert Smith" ))"
cursor.execute(v_sql)

The problem is the " around Robert Smith in the SQL. 问题在于SQL中的Robert Smith。

You would escape the quotes or use a different quoting style. 您将转义引号或使用其他引号样式。 Single quotes would do here, for example. 例如,单引号将在此处执行。

Using single quotes: 使用单引号:

v_sql = 'SELECT DISTINCT tblSeller.ID, tblSeller.Navn FROM tblResult INNER JOIN tblSeller ON tblResult.SellerID = tblSeller.ID WHERE (((tblSeller.Name)="Robert Smith" ))'

Using escaping: 使用转义:

v_sql = "SELECT DISTINCT tblSeller.ID, tblSeller.Navn FROM tblResult INNER JOIN tblSeller ON tblResult.SellerID = tblSeller.ID WHERE (((tblSeller.Name)=\"Robert Smith\" ))"

or you can use triple quotes (one of ''' or """ ); these allow for easy multi-line strings as newlines are allowed and included in the final string value: 或者,您可以使用三重引号( '''""" );由于允许使用换行符并将其包含在最终的字符串值中,因此这些引号允许使用简单的多行字符串:

v_sql = """
SELECT DISTINCT tblSeller.ID, tblSeller.Navn 
FROM tblResult INNER JOIN tblSeller ON tblResult.SellerID = tblSeller.ID
WHERE (((tblSeller.Name)=\"Robert Smith\" ))
"""

Try mixing with single quotes: 尝试混合使用单引号:

v_sql = "SELECT DISTINCT tblSeller.ID, tblSeller.Navn FROM tblResult INNER JOIN tblSeller ON tblResult.SellerID = tblSeller.ID WHERE (((tblSeller.Name)='Robert Smith' ))"

or 要么

v_sql = 'SELECT DISTINCT tblSeller.ID, tblSeller.Navn FROM tblResult INNER JOIN tblSeller ON tblResult.SellerID = tblSeller.ID WHERE (((tblSeller.Name)="Robert Smith" ))'

In python strings can either be quoted with a doublequote " or a singlequote ' . This is very usefull in these cases. 在python中,字符串可以用双引号"或单引号"引起' ,这在这些情况下非常有用。

In Python you can create string using ' or " as other says. 在Python中,你可以使用创建的字符串'"其他说。

But in this case I would like to propose using PreparedStatement instead of simple query. 但是在这种情况下,我想建议使用PreparedStatement而不是简单查询。 Such PreparedStatement uses ? 这样的PreparedStatement用途是? in place of arguments and arguments are simply array: 代替参数,参数只是数组:

v_sql = "SELECT DISTINCT tblSeller.ID, tblSeller.Navn FROM tblResult INNER JOIN tblSeller ON tblResult.SellerID = tblSeller.ID WHERE (((tblSeller.Name)=?))"
rs = c.execute(v_sql, ["Robert Smith", ])

PreparedStatements have many advantages: they are simpler to database engines to parse, and cache query plans, they are SQLInjection attacks safe etc. PreparedStatement具有许多优点:数据库引擎可以更轻松地解析它们并缓存查询计划,它们可以安全地进行SQLInjection攻击。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM