こんにちは私はテンプレートから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が無効なときにはどうしますか? – ysth