pythonmysqlstored-proceduresunattended-processing

Call a MySQL stored procedure from Python unattended


I need to call a MySQL stored procedure from Python, and I don't need to wait for the procedure to finish.

How can this be done?


Solution

  • code below work for me

    import mysql.connector
    
    def insertComment(ecID, eID, eComment):
        try:
            contraseña = input("Please, enter your database password: ")
            connection = mysql.connector.connect(host='localhost',
                                                 database='mantenimiento',
                                                 user='hernan',
                                                 password=contraseña,
                                                 port=3309)
            if connection.is_connected():
                cursor = connection.cursor(prepared=True)
                procedure = "call mantenimiento.spSaveComment(%s, %s, %s)"
                datos = (ecID, eID, eComment)
                cursor.execute(procedure, datos)
                # datos = [(ecID, eID, eComment)]  # Tuple for executemany
                # cursor.executemany(procedure, datos)
                connection.commit()
                print(cursor.rowcount, "Comment inserted sucessfully")
        except mysql.connector.Error as error:
            connection.rollback()
            print("Failed to insert value into database {}".format(error))
        finally:
            if (connection.is_connected()):
                cursor.close()
                connection.close()
                print("Server connection was closed")
    
    
    insertComment(15, 25, 'Test MariaDB or MySQL SP from python')