1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
import csv import pymysql
filename = "***************\\minsu_info.csv"
with open(filename, 'r', encoding="utf-8") as f: reader = csv.reader(f) data = list(reader) data.pop(0)
db = pymysql.connect('182.92.226.**', 'root', '密码', 'theOld') cursor = db.cursor()
i = 0 for dd in data: i += 1 sql1 = "SELECT id from user_userInfo WHERE nickname = %s" if dd[1] == '佚名': dd[1] = '官方2号' cursor.execute(sql1, dd[1]) results = cursor.fetchall() if results: sql2 = "INSERT INTO main_data_tougaolanmu(secondTypeId,name,userId,content,modifiedTime) VALUES(38,%s,%s,%s,NOW())" dad = dd dad[1] = results[0][0] try: cursor.execute(sql2, dad) db.commit() except: print('出错1') db.rollback() else: sql3 = "INSERT INTO user_userInfo(nickname) VALUES(%s)" try: cursor.execute(sql3, dd[1]) db.commit() except: print('出错2') db.rollback() cursor.execute(sql1, dd[1]) result = cursor.fetchall() sql4 = "INSERT INTO main_data_tougaolanmu(secondTypeId,name,userId,content,modifiedTime) VALUES(38,%s,%s,%s,NOW())" dad = dd dad[1] = result[0][0] try: cursor.execute(sql4, dad) db.commit() except: print('出错3') db.rollback() print(i, results)
db.close
|