0

I'm not sure if this is something small that I just cannot see. I'm new to all this SQL in Python and can't see why the transaction is not running in my database. Hope you can help!

#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()
7
  • Wrap the transaction in a try/except block and see if an error is being returned Commented Jan 23, 2019 at 23:45
  • Hi. Sorry for this. How would I do this? Commented Jan 24, 2019 at 11:31
  • Here's an example: stackoverflow.com/questions/30996401/… Commented Jan 24, 2019 at 19:41
  • Hmm, that didn't seem to do anything. Commented Jan 29, 2019 at 12:39
  • @Steve tried the try statement. Breaking the connection to the db brought back errors as expected. Running the query with the db connection resolved did not return any errors. Commented Jan 29, 2019 at 19:11

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.