只有mysql执行呢,不用定时任务。执行一定次数后停止
时间: 2024-09-20 13:12:57 浏览: 35
如果你仅想在Python环境中通过pymysql库执行MySQL SQL,并且设定执行特定次数后停止,你可以创建一个循环结构。下面是一个简单的例子,假设你想执行10次:
```python
import pymysql
# MySQL连接信息
host = "your_host"
user = "your_user"
password = "your_password"
db = "your_database"
# SQL查询
sql_query = '''
INSERT INTO task_book_scan_flight (
flt_Num, route, fleet_Cd, tail_Nr, flight_Type, flt_Dt,
report_Seq_Num, staff_Num, staff_Name, scan_tm,
scan_arp_cd, entrance, sofl_Seq_Nr, data_source
) VALUES (
..., ...
)
'''
# 执行次数
max_executions = 10
for i in range(1, max_executions + 1):
with pymysql.connect(host=host, user=user, password=password, db=db) as connection:
with connection.cursor() as cursor:
cursor.execute(sql_query)
# 如果需要,可以打印执行日志
print(f"Execution {i}: {cursor.rowcount} rows inserted")
if i == max_executions: # 当达到最大执行次数时退出循环
break
```
这个脚本会连续执行10次SQL插入,然后终止。你需要将上述代码中的`your_host`, `your_user`, `your_password`, 和 `your_database` 替换为实际的MySQL服务器配置。
阅读全文