我是python的新手,并且在SQL查询(MSSQL 2008 R2)中转义字符时遇到一些问题。文件中有一些行用作查询字符串的输入,该字符串具有单引号,括号和其他字符。他们是电视和电影:
泰勒佩里的爸爸的小女孩玩家 - 3D
美国团聚(' 12)(未评级)
Jarhead 2:火场(未分级)
我不太确定如何正确使用转义字符。这是我的剧本:
#!/usr/bin/python
import pyodbc
BASE_QUERY = """
SELECT AssetID,AssetName,AssetTypeID,DescriptorXML
FROM AssetLibrary.dbo.ALAsset with(nolock)
WHERE IsDeleted = 0
and (AssetTypeID = 1 or AssetTypeID = 4)
and AssetName like '%%%s%%'
"""
List = []
with open ("C:/Users/josh/Desktop/title-list.txt", "r") as potato:
with pyodbc.connect(DSN='SQL-v13') as con:
cur = con.cursor()
for line in potato:
query = BASE_QUERY % line
cur.execute(query)
results = list(cur.fetchall())
if len(results) > 0:
List.append(results)