Today when the storage of the error Pymysql.err.InterfaceError: (0, "), after troubleshooting, found that the connection to the database code to the outside of the insertion function, resulting in multi-threaded operation error
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()
That is, the 2, 3 lines are placed on the outside of the function, and the write_into_db function is put in the main function of the thread, if placed outside the function, will cause the database can not be connected
MySQL Database error Pymysql.err.InterfaceError: (0, ")