標籤:and col connect 線上 cti art cep rate span
今天入庫的時候出現了報錯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函數是放線上程主函數中的,如果放在函數外面,就會導致無法串連資料庫
MySQL資料庫報錯pymysql.err.InterfaceError: (0, '')