两张相似表的合并

因为两个程序爬取了两个网页的信息产生两个表,对表操作时不方便所以想到合并表,作者数据库不熟练不能写出sql语句进行合并,因此想到程序合并。原理是将A表数据全部查询出来,再把每一条的主键跟B对比如果存在则更新,不存在就插入。源码如下:

import pymysql
db = pymysql.connect(host='127.0.0.1',port=3306,user='root',password='******',db='jl_pdsustu',charset='utf8')
cursor = db.cursor()
cursor2 = db.cursor()
cursor3 = db.cursor()
sql = 'select * from stuinfo2'
try:
    cursor.execute(sql)
    row = cursor.fetchone()
    while row:
        sql2 = 'select * from allstuallinfo where stuid =' + row[0]
        if cursor2.execute(sql2) == 1:
            print(row)
            sql3 = "UPDATE allstuallinfo SET " \
                   "education = (SELECT education FROM stuinfo2 WHERE stuid = '%s'), " \
                   "school_system = (SELECT school_system FROM stuinfo2 WHERE stuid = '%s'), " \
                   "id = (SELECT id FROM stuinfo2 WHERE stuid = '%s'), " \
                   "phone2 = (SELECT phone FROM stuinfo2 WHERE stuid = '%s'), " \
                   "email2 = (SELECT email FROM stuinfo2 WHERE stuid = '%s') " \
                   "WHERE stuid = '%s'" % (row[0],row[0],row[0],row[0],row[0],row[0])
            db.query(sql3)
            db.commit()
            print("更新成功")
        else:
            sql4 = "insert into allstuallinfo" \
                   "(stuid,name,sex,stuyear,garde,college,major,class,education,school_system,id,phone2,email2,photo) " \
                   "SELECT * FROM stuinfo2 WHERE stuid = '%s'" % (row[0])
            db.query(sql4)
            db.commit()
            print("插入成功")
        row = cursor.fetchone()


except:
    print('error')
    
db.close()

猜你喜欢

转载自blog.csdn.net/L141210113/article/details/80994667