0
У меня есть этот питон функция:открытие и закрытие MySQL соединение
import MySQLdb as mdb
def sql_query(sql):
try:
con = mdb.connect('localhost', 'user', '', 'dbs')
with con:
cur = con.cursor()
cur.execute(sql)
results = cur.fetchall()
return results
except mdb.Error, e:
sys.exit(1)
finally:
if con:
con.close()
тогда я заряжаем код, который вызывает эту функцию, когда-то зэк закрывает и я получаю это отслеживающий:
Traceback (most recent call last):
File "users.py", line 431, in <module>
orders = get_orders(user_id, mongo_user_id, address_book_list)
File "users.py", line 343, in get_orders
order_lines = get_order_lines(order_id)
File "users.py", line 288, in get_order_lines
for item in sql_query(get_order_lines_sql):
File "users.py", line 57, in sql_query
if con:
UnboundLocalError: local variable 'con' referenced before assignment
где мой get_orders
код, как:
def get_orders(user_id, mongo_user_id, address_book_list):
# we just want orders that are less than 2 years old
get_orders_sql = """SELECT order_id, state, state2, stamp, reference, comments, address_id, delivery_id, user_id FROM user_orders WHERE address_id IS NOT NULL and stamp >= '2012-01-01 00:00:00' and user_id='%s' ORDER BY stamp DESC"""% (user_id)
# delivery_id - this is the shipping option the user chose.
for items in sql_query(get_orders_sql):
#print items
order_id = items[0]
есть около 50K строк в заказах
Какой совет высоко ценится