从 Python 无人参与的情况下调用 MySQL 存储过程
Call a MySQL stored procedure from Python unattended
我需要从 Python 调用 MySQL 存储过程,我不需要等待该过程完成。
如何做到这一点?
一种可能的解决方案是使用 celery:"Celery is an asynchronous task queue/job queue based on distributed message passing."。您可以在调用 MySQL 存储过程的地方创建一个任务。
下面的代码对我有用
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')
我需要从 Python 调用 MySQL 存储过程,我不需要等待该过程完成。
如何做到这一点?
一种可能的解决方案是使用 celery:"Celery is an asynchronous task queue/job queue based on distributed message passing."。您可以在调用 MySQL 存储过程的地方创建一个任务。
下面的代码对我有用
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')