首页 > 解决方案 > 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()

标签: pythonmysql

解决方案


推荐阅读