【发布时间】:2016-09-22 17:30:20
【问题描述】:
您好,我有以下函数可以从我的模板中获取 sql。变量行正在获取用户输入的查询。如果用户输入了一个无效的 sql,我会得到一个错误 UnboundLocalError: local variable 'row' referenced before assignment (因为没有行,因为 sql 是错误的)我怎样才能有效地处理这个错误?对 django python 有点陌生。可以帮我解决这个问题吗?提前致谢。
def DBQuery(sql):
c = MySQLdb.connect(host=HOST,user=USER,passwd=PASS,db=DB, cursorclass=MySQLdb.cursors.DictCursor)
cursor = c.cursor()
try:
cursor.execute(sql)
row = cursor.fetchall()
except Exception, e:
print "Error found!", e
cursor.close()
c.close()
return row
【问题讨论】:
-
那么,当 SQL 无效时,您希望发生什么?
标签: python mysql django exception fetchall