1

I have run into a very strange issue where my code below was working (I was able to add a table into the DB). After dropping a test table via the mysql workbench I am able to add a database and the code continues to run with no errors but no table is added. I have slimmed the SQL right down and attempted on different dbs. I have also restarted. Am I doing something wrong here? Any help is greatly appreciated.

import mysql.connector
import os

dirname = os.path.abspath('')
sql_filename = dirname + '\SQL_Creation\Test.sql'

class connectionsetup:
    def __init__(self):
      self.mydb = mysql.connector.connect(
      host="localhost",
      user="root",
      password="")
    
      try:
        self.mycursor = self.mydb.cursor()
        print('Conneciton Succesful')
        self.mycursor.execute("CREATE DATABASE AP_Application_Db_test")
        print('Database Created')
        self.mycursor.execute('USE ap_application_db_test; Create TABLE test (SERIAL_NUMBER VARCHAR(255),VIOLATION_STATUS VARCHAR(255))', multi=True)
        self.mydb.commit()
        self.mydb.close()

      except mysql.connector.Error as err:
        print(err)
        print("Error Code:", err.errno)
        print("SQLSTATE", err.sqlstate)
        print("Message", err.msg   )
        
    
connectionsetup()

1 Answer 1

2

According to the docs, cursor.execute(..., multi=True)

[...] returns an iterator that enables processing the result of each statement

So the code needs to be like this:

for _ in self.mycursor.execute(multiple_sql_statements, multi=True):
    pass
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @snakecharmerb that now works consistently.

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.