Call a MySQL stored procedure from Python unattended

622 views Asked by At

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?

2

There are 2 answers

1
Hernan Torres On BEST ANSWER

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')
1
Manuel Carrero On

One possible solution is by using celery: "Celery is an asynchronous task queue/job queue based on distributed message passing.". You can create a task where you call your MySQL store procedure.