使用python对MySQL两个表进行对比

在Ubuntu系统下,已知在MySQL的同一个库中有两个表名称为old_file、new_file,两个表结构一样,都有两列数据,两列分别为“file”,“ content”。
假设两张表内容如下
old_file

img


new_file

img


如何使用python在两个表中搜索file列相同的,然后对其对应的content列进行对比,如果content不相同则执行命令如“echo $file ”,如果相同则跳过,如果old_file有new_file没有的file列,输出命令“echo new_file not find this $file”

您好,请参考以下代码,如有帮助,还请采纳。

import pymysql
import pandas as pd
import os
# 请自行设置数据库参数
conn = pymysql.connect(host='xxx', port=3306, user='xxx', password='xxx', db='xxx', charset='utf8')
sql1 = "select * from old_file"
sql2 = "select * from new_file"
df1 = pd.read_sql(sql1, conn)
df2 = pd.read_sql(sql2, conn)
conn.close()


for f in df1.file.values:
    if f in df2.file.values:
        c1 = df1.loc[df1['file'] == f].loc[:, 'content'].values[0]
        c2 = df2.loc[df2['file'] == f].loc[:, 'content'].values[0]
        if c1 == c2:
            pass
        else:
            s = "echo \"" + f + "\""
            os.system(s)
    else:
        print("echo new_file not find this $" + f)