-2

我需要从 Python 调用 MySQL 存储过程,并且不需要等待过程完成。

如何才能做到这一点?

4

2 回答 2

0

下面的代码对我有用

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')
于 2020-04-12T12:10:06.423 回答
0

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.

于 2019-06-16T22:34:59.697 回答