您好,我尝试在 django 中使用 mysql 准备好的语句。
我做了以下课程:
class PreparedStatement(object):
def __init__(self,name,query):
self.name = name
self.query = query
self.vars = []
self.prepare()
def setVar(self,name,var):
name = "@%s" % name
if name not in self.vars:
self.vars.append(name)
SQL = "SET %s = " % (name)
self.__executeQuery(SQL+" %s;",var)
def prepare(self):
SQL = "PREPARE %s FROM " % self.name
self.__executeQuery(SQL + " %s ;", self.query)
def execute(self):
SQL = "EXECUTE %s " % self.name
if len(self.vars):
params = ""
for var in self.vars:
params += var + ", "
params = params[:-2]
SQL += "USING %s " % params
result = self.__executeQuery(SQL)
self.vars = []
return result
def __executeQuery(self,query,*args):
cursor = connection.cursor()
if args:
cursor.execute(query,args)
else:
cursor.execute(query)
return cursor
我像这样使用它:
getDiscountsById = PreparedStatement("getDiscountsById","""SELECT * FROM table
WHERE id = ? LIMIT 1""")
getDiscountsById.setVar("id",5)
result = getDiscountsById.execute()
它在第一个加载的页面上工作正常,但是在更改 url 或重新加载页面后 MySQL 返回准备好的语句未找到错误。
问题可能出在哪里?有什么解决办法吗?
感谢您的回复,对不起我的英语:D