我有一个用于执行查询的 python MySQLdb 包装器。每当我使用此包装器在字符串字段中执行包含“%”符号的查询时,都会收到 TypeError。
class DataBaseConnection:
def __init__(self,dbName='default'
):
#set
dbDtls={}
dbDtls=settings.DATABASES[dbName]
self.__host=dbDtls['HOST']
self.__user=dbDtls['USER']
self.__passwd=dbDtls['PASSWORD']
self.__db=dbDtls['NAME']
self.dbName=dbName
def executeQuery(self, sqlQuery, criteria=1):
""" This method is used to Execute the Query and return the result back """
resultset = None
try :
cursor = connections[self.dbName].cursor()
cursor.execute(sqlQuery)
if criteria == 1:
resultset = cursor.fetchall()
elif criteria == 2:
resultset = cursor.fetchone()
transaction.commit_unless_managed(using=self.dbName)
#cursor.execute("COMMIT;")
#objCnn.commit()
cursor.close()
#objCnn.close()
except Exception,e:
resultset = False
objUtil=Utility()
error=''
error=str(e)+"\nSQL:"+sqlQuery
print error
objUtil.WriteLog(error)
del objUtil
return resultset
sql = """SELECT str_value FROM tbl_lookup WHERE str_key='%'"""
objDataBaseConnection = DataBaseConnection()
res = objDataBaseConnection.executeQuery(sql)
print res
我尝试转义 '%' 字符,但没有成功。数据库字段是 VARCHAR 字段。