用python写一段代码,连接mysql数据库1和数据库2,求出df1和df2的差集,最后输出:id列,期望32,实际:31
时间: 2024-09-15 22:03:59 浏览: 32
DF1协议概述硬件配置与连接.pdf
在Python中,我们可以使用`pandas`库连接到MySQL数据库并执行SQL查询来获取数据。首先,你需要安装`pymysql`和`sqlalchemy`库,如果还没有的话。然后,你可以按照以下步骤编写代码:
```python
import pandas as pd
from sqlalchemy import create_engine
# 创建连接字符串,假设你有如下格式:
db_config_mysql1 = {
"host": "localhost",
"port": 3306,
"username": "your_username1",
"password": "your_password1",
"database": "database1"
}
db_config_mysql2 = {
"host": "localhost",
"port": 3306,
"username": "your_username2",
"password": "your_password2",
"database": "database2"
}
# 连接两个数据库
engine1 = create_engine(f'mysql+pymysql://{db_config_mysql1["username"]}:{db_config_mysql1["password"]}@{db_config_mysql1["host"]}:{db_config_mysql1["port"]}/{db_config_mysql1["database"]}')
engine2 = create_engine(f'mysql+pymysql://{db_config_mysql2["username"]}:{db_config_mysql2["password"]}@{db_config_mysql2["host"]}:{db_config_mysql2["port"]}/{db_config_mysql2["database"]}']
# 从两个数据库中分别读取数据
df1 = pd.read_sql_query("SELECT id FROM table_name1", engine1)
df2 = pd.read_sql_query("SELECT id FROM table_name2", engine2)
# 计算差集
diff_ids = df1[~df1['id'].isin(df2['id'])]
# 输出结果
print("期望32:", len(diff_ids))
print("实际31:", len(diff_ids[df1['id'] == 31])) # 如果存在id=31的情况,可以进一步检查
```
请替换上述代码中的数据库配置信息(如主机、端口、用户名、密码和表名),并且确保table_name1和table_name2是你想要比较的实际表名。
阅读全文