MySQL数据库报错pymysql.err.InterfaceError: (0, '')

时间:2018-04-27 19:48:17   收藏:0   阅读:10186

今天入库的时候出现了报错pymysql.err.InterfaceError: (0, ‘‘),经过排查,发现是由于把连接数据库的代码放到了插入函数的外部,导致多线程运行出错

 1 def write_into_db(data):
 2     db = pymysql.connect(host=db_host, user=db_user, password=db_password, port=db_port, db=db_name, charset=utf8)
 3     cursor = db.cursor()
 4     print(start inserting into db)
 5     print(data)
 6     t = datetime.now()
 7     t = str(t).split(.)[0]
 8     sql = "INSERT INTO fixed_asset_new (resource_id,resource_type,name,address,location,land_use,sell_type,land_type,is_bid,deal_status,source_url,province,city,district,plot_rate,declaration_time,start_time,expiration_time,start_price,transaction_price,min_raise_price,cash_deposit,trading_place,assignee,fixture_time,consult_tel,subject_type,housing_area,land_area,evaluate_price,auction_stage,memo,is_deleted,gmt_created,gmt_modified) VALUES (%d,%d,‘%s‘,‘%s‘,‘%s‘,%d,%d,%d,%d,%d,‘%s‘,‘%s‘,‘%s‘,‘%s‘,‘%s‘,‘%s‘,‘%s‘,‘%s‘,%d,%d,%d,%d,‘%s‘,‘%s‘,‘%s‘,‘%s‘,%d ,‘%s‘,‘%s‘,%d,%d,‘%s‘,%d,‘%s‘,‘%s‘)" % (data[resource_id],data[resource_type],data[name],data[address],data[location],data[land_use],data[sell_type],data[land_type],0,data[deal_status],data[source_url],data[province],data[city],data[district],data[plot_rate],data[declaration_time],data[start_time],data[expiration_time],data[start_price],data[transaction_price],data[min_raise_price],data[cash_deposit],data[trading_place],data[assignee],data[fixture_time],data[consult_tel],data[subject_type],data[housing_area],data[land_area],data[evaluate_price],data[auction_stage],data[memo],data[is_deleted],t,t)
 9     try:
10         cursor.execute(sql)
11         db.commit()
12         print(successful)
13     except Exception as e:
14         print(e)
15         db.rollback()

也就是把2,3行代码放在了函数外面,而write_into_db函数是放在线程主函数中的,如果放在函数外面,就会导致无法连接数据库

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!