def sx(): sql = 'truncate weather' cursor.execute(sql) start_shuaxin = r"D:\python\pythonProject\shuaxin.py" os.system('python %s' % start_shuaxin) return render_template('/user/diqu.html')
时间: 2024-03-31 10:32:00 浏览: 8
这段代码中,`sx()` 函数的作用是清空数据库中的 `weather` 表,并启动一个名为 `shuaxin.py` 的 Python 脚本。最后,函数将返回一个名为 `diqu.html` 的模板文件。
具体来说,该函数执行以下操作:
- 使用 SQL 语句 `truncate weather` 清空数据库中的 `weather` 表。
- 使用 `os.system()` 函数运行 `shuaxin.py` 脚本。该脚本的路径为 `D:\python\pythonProject\shuaxin.py`,使用 `python` 命令运行。
- 返回名为 `diqu.html` 的模板文件,用于呈现用户界面。
如果你有任何问题或需要进一步帮助,请随时告诉我。
相关问题
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
在这段代码中,有几个问题需要修复:
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)
```
没有数据生成插入 CREATE OR REPLACE PROCEDURE ord_dev_3 IS v_month NUMBER; v_sql VARCHAR2(4000); CURSOR v_dt IS SELECT DISTINCT dd FROM ycyd.tables_ord ORDER BY dd; BEGIN DELETE FROM tables_ord; INSERT INTO tables_ord SELECT dd FROM (SELECT MAX(to_number(SUBSTR(TABLE_NAME, '15'))) dd FROM SYS.ALL_TABLES a WHERE a.owner = upper('jour1') AND a.table_name LIKE upper('om_subscriber_202___')) aa WHERE aa.dd > 202205; COMMIT; /* v_sql := 'truncate table ycyd.ord_dev_3056'; EXECUTE IMMEDIATE v_sql; COMMIT;*/ FOR v_month IN 202206 .. 202312 LOOP v_sql := 'truncate table ycyd.ord_dev_3056'; EXECUTE IMMEDIATE v_sql; v_sql := 'insert into ycyd.ord_dev_3056 SELECT a.order_id, a.done_date, a.cust_id, a.subscriber_ins_id, a.access_num, c.dev_id, c.dev_name, c.corp_org, c.company_name, c.department_code, c.department_name, c.remarks FROM JOUR1.Om_Subscriber_' || v_month || ' a LEFT JOIN JOUR1.om_order_' || v_month || ' b ON a.order_id = b.order_id LEFT JOIN params1.sec_developer c ON b.dev_id = c.dev_id WHERE 1 = 1 AND a.access_num LIKE ''192%'' AND a.action = 0'; /*EXECUTE IMMEDIATE v_sql;*/ END LOOP; EXECUTE IMMEDIATE v_sql; COMMIT; END ord_dev_3;
这段代码是一个 Oracle 存储过程,它的目的是根据一些条件生成插入数据的 SQL 语句。下面我会对这段代码进行解释:
- 首先,通过声明存储过程 `ord_dev_3`,定义了一些变量和游标。
- 游标 `v_dt` 从表 `ycyd.tables_ord` 中选择所有不重复的 `dd` 列,并按照 `dd` 进行排序。
- 接着,执行 `DELETE FROM tables_ord;` 语句,清空表 `tables_ord` 中的数据。
- 然后,执行 `INSERT INTO tables_ord SELECT dd ...` 语句,将符合条件的数据插入到表 `tables_ord` 中。具体的条件是从 `SYS.ALL_TABLES` 视图中获取最大的数字,并根据一些条件进行筛选。
- 在注释部分,有一段被注释掉的代码 `/* v_sql := 'truncate table ycyd.ord_dev_3056'; ... */`,它是对表 `ycyd.ord_dev_3056` 进行截断操作的语句,你可以根据需要解除注释并执行该操作。
- 紧接着是一个循环,从 202206 到 202312 的范围内,依次取值给变量 `v_month`。
- 在循环体内,根据当前的 `v_month` 值拼接动态 SQL 语句到变量 `v_sql` 中。
- 最后,执行 `EXECUTE IMMEDIATE v_sql;` 语句,执行动态 SQL 语句。
请注意,这段代码中有一些被注释掉的语句,你可以根据需求解除注释并执行相应的操作。另外,代码中还有一些表名和列名需要根据实际情况进行替换。执行这段代码前,请确保你已经了解代码的逻辑,并对其中的 SQL 语句进行适当的修改和调整。