python - Python MySQL 多查询
问题描述
我不确定这是否是我看不到的小东西。我是 Python 中所有这些 SQL 的新手,不明白为什么事务没有在我的数据库中运行。希望你能帮忙!
#requires mysql.connector
import mysql.connector
#make the sql connection
mydb = mysql.connector.connect(
host="localhost",
user="root",
passwd="",
db="networks"
)
#create our cursor element
cursor=mydb.cursor()
#lets loop through the table
#run our query to move data in the db to the archive
cursor.execute("""START TRANSACTION;
INSERT INTO archive select * from data where cdate < (NOW() - INTERVAL 1 MONTH);
DELETE FROM archive WHERE cdate < (NOW() - INTERVAL 6 MONTH);
DELETE FROM data;
COMMIT;
""", multi=True)
#select all from devices
cursor.execute("SELECT * FROM devices")
#do something with this query
for row in cursor.fetchall():
#Line for testing output from SQL table
print("Connecting to "+row[1])
#here is where we run our SSH Code
#code from here will re-populate the data table
#make sure we close everything
mydb.commit()
cursor.close()
mydb.close()
解决方案
推荐阅读
- c# - 测试字符串值是否包含在字符串数组中
- python - Flask SocketIO 不向分配的房间发送消息
- blockchain - 在 React Typescript Web 应用程序(克隆的 create-daml-app)上的 DABL 项目中使用派对 ID
- java - 生成大量小pdf文件
- javascript - Javascript - 检测广告拦截器检测器
- matlab - 在 Matlab 中求解积分符号
- angular - 选择日期选择器材质后如何格式化数据?
- python - 比较每个项目中具有多个值的两个列表
- pdf - Libreoffice Writer(无头和非无头)在将 pdf 导出为 PDF/A 后删除其签名
- java - 使用 postDelayed 延迟打开片段是一个好习惯吗?