import pymysql import time while True: # 连接源数据库 src_conn = pymysql.connect(host='10.43.64.110', port=3306, user='selectuser', password='Xy@123456', database='messpdb') print("连接源数据库成功") # 连接目标数据库 dst_conn = pymysql.connect(host='10.43.144.231', port=3306, user='root', password='123456', database='czjsc') print("连接目标数据库成功") # 创建源游标对象 src_cursor = src_conn.cursor() # 创建目标游标对象 dst_cursor = dst_conn.cursor() # 编写SQL查询语句 #各牌号烟丝总重量 sql1 = 'select mat_id,material_name ,ROUND (sum(quantity) ,1 ) weight ,unit_id from messpdb.silk_stock a left join messpdb.maindata_material b on a.mat_id =b.ctrl where mat_id >0 group by mat_id,material_name, unit_id' sql2 = "select ROUND (sum(quantity) ,1 ) weight ,unit_id from messpdb.silk_stock a where mat_id >0 group by unit_id" sql3 = "select aa.ids,ROUND (aa.c1/bb.c2 ,3 )*100 from (select 1 as ids,count(box_code) c1 from messpdb.silk_stock a where mat_id >0 group by ids) aa, (select 1 as ids,count(box_code) c2 from messpdb.silk_stock a group by ids)bb where aa.ids=bb.ids" # 执行SQL查询语句 src_cursor.execute(sql1) src_cursor.execute(sql2) src_cursor.execute(sql3) # 获取查询结果 results1 = src_cursor.fetchall() results2 = src_cursor.fetchall() results3 = src_cursor.fetchall() print("查询数据库成功") # 更新数据到目标数据库 for row in results1: mat_id = row[0] material_name = row[1] weight = row[2] unit_id = row[3] # 将数据更新到目标数据库中 update_sql = "update cs_list set material_name=%s, weight=%s, unit_id=%s where mat_id=%s" dst_cursor.execute(update_sql, (material_name, weight, unit_id, mat_id)) print("更新数据成功") # 更新数据到目标数据库 for row in results2: weight = row[0] unit_id = row[1] # 将数据更新到目标数据库中 update_sql = "update cs2_list set weight=%s, unit_id=%s where id=1" dst_cursor.execute(update_sql, (weight, unit_id)) for row in results3: ids = row[0] stock_ratio = row[1] # 将数据更新到目标数据库中 update_sql = "update cs3_list set stock_ratio=%s where id=1" dst_cursor.execute(update_sql, stock_ratio) # 提交事务并关闭连接 dst_conn.commit() dst_cursor.close() src_cursor.close() dst_conn.close() src_conn.close() print("断开数据库连接成功") time.sleep(1) 帮我debug
时间: 2024-02-27 20:54:54 浏览: 64
python-mysql.zip_MYSQL_pymysql_python MySQL_python连接mysql_连接数据库
5星 · 资源好评率100%
在这段代码中,有几个问题需要修复:
1. `src_cursor.execute(sql1)`只执行了第一个SQL查询,后面的两个SQL查询也需要执行,可以将三个查询语句合并成一个查询语句,或者分别执行三次查询语句并将结果保存到不同的变量中。
2. 在更新数据到目标数据库时,对于第二个查询结果和第三个查询结果,更新语句中的where条件都是固定的,这样会导致每次更新都是更新同一行的数据,需要修改where条件。
3. 在更新数据到目标数据库之前,需要先将目标游标对象dst_cursor设置为可接受多条SQL语句,如下所示:
```
dst_cursor = dst_conn.cursor(pymysql.cursors.DictCursor)
```
4. 在更新数据到目标数据库之前,需要先将目标表中的数据清空,可以使用如下语句:
```
truncate_sql = "truncate table cs_list"
dst_cursor.execute(truncate_sql)
```
综上所述,修改后的代码如下所示:
```
import pymysql
import time
while True:
# 连接源数据库
src_conn = pymysql.connect(host='10.43.64.110', port=3306, user='selectuser', password='Xy@123456', database='messpdb')
print("连接源数据库成功")
# 连接目标数据库
dst_conn = pymysql.connect(host='10.43.144.231', port=3306, user='root', password='123456', database='czjsc')
print("连接目标数据库成功")
# 创建源游标对象
src_cursor = src_conn.cursor()
# 创建目标游标对象
dst_cursor = dst_conn.cursor(pymysql.cursors.DictCursor)
# 清空目标表中的数据
truncate_sql = "truncate table cs_list"
dst_cursor.execute(truncate_sql)
# 编写SQL查询语句
sql = '''
select a.mat_id, b.material_name, ROUND(sum(a.quantity), 1) as weight, a.unit_id
from messpdb.silk_stock a
left join messpdb.maindata_material b on a.mat_id = b.ctrl
where a.mat_id > 0
group by a.mat_id, b.material_name, a.unit_id;
select ROUND(sum(a.quantity), 1) as weight, a.unit_id
from messpdb.silk_stock a
where a.mat_id > 0
group by a.unit_id;
select aa.ids, ROUND(aa.c1 / bb.c2, 3) * 100 as stock_ratio
from (
select 1 as ids, count(box_code) as c1
from messpdb.silk_stock a
where a.mat_id > 0
group by ids
) aa, (
select 1 as ids, count(box_code) as c2
from messpdb.silk_stock a
group by ids
) bb
where aa.ids = bb.ids;
'''
# 执行SQL查询语句
src_cursor.execute(sql, multi=True)
# 获取查询结果
results = src_cursor.fetchall()
print("查询数据库成功")
# 更新数据到目标数据库
for row in results[0]:
mat_id = row["mat_id"]
material_name = row["material_name"]
weight = row["weight"]
unit_id = row["unit_id"]
# 将数据更新到目标数据库中
update_sql = "insert into cs_list (mat_id, material_name, weight, unit_id) values (%s, %s, %s, %s)"
dst_cursor.execute(update_sql, (mat_id, material_name, weight, unit_id))
for row in results[1]:
weight = row["weight"]
unit_id = row["unit_id"]
# 将数据更新到目标数据库中
update_sql = "update cs2_list set weight=%s, unit_id=%s where id=1"
dst_cursor.execute(update_sql, (weight, unit_id))
for row in results[2]:
ids = row["ids"]
stock_ratio = row["stock_ratio"]
# 将数据更新到目标数据库中
update_sql = "update cs3_list set stock_ratio=%s where id=1"
dst_cursor.execute(update_sql, stock_ratio)
# 提交事务并关闭连接
dst_conn.commit()
dst_cursor.close()
src_cursor.close()
dst_conn.close()
src_conn.close()
print("断开数据库连接成功")
time.sleep(1)
```
阅读全文