Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python sql连接器执行简单存储过程_Python_Mysql_Mysql Connector Python - Fatal编程技术网

使用python sql连接器执行简单存储过程

使用python sql连接器执行简单存储过程,python,mysql,mysql-connector-python,Python,Mysql,Mysql Connector Python,我在mysql数据库上有一个非常简单的存储过程,使用4个VARCHAR参数并向某个表中执行insert操作(我这样做的原因是为了能够更好地控制我给应用程序的权限)。当我直接从mysql调用该过程时,如下所示: CALL my_proc('aa', 'bb', 'cc', 'dd'); 该过程完成其工作,并将记录插入到相关表中。现在,当我通过python(使用mysql.connector)尝试时,代码似乎执行得很好(没有错误),但它没有执行预期的insert语句。我的代码是这样的: from

我在mysql数据库上有一个非常简单的存储过程,使用4个VARCHAR参数并向某个表中执行insert操作(我这样做的原因是为了能够更好地控制我给应用程序的权限)。当我直接从mysql调用该过程时,如下所示:

CALL my_proc('aa', 'bb', 'cc', 'dd');
该过程完成其工作,并将记录插入到相关表中。现在,当我通过python(使用mysql.connector)尝试时,代码似乎执行得很好(没有错误),但它没有执行预期的insert语句。我的代码是这样的:

from mysql.connector import MySQLConnection, Error, Warning
from helpers import get_config


def connect():
    # helper procedure to fetch credentials from config.ini
    db_config = get_config('mysql')

    try:
        print('Connecting...')
        conn = MySQLConnection(**db_config)

        if conn.is_connected():
            print('connected')
            return conn
        else:
            print('could not connect')
            return None

    except Error as e:
        print(e)


def insert_record(conn, a, b, c, d):
    try:
        args = [a, b, c, d]
        cursor = conn.cursor()
        print('inserting {0} into database ''{1}'''.format(args, conn.database))
        statement = "CALL my_proc('{0}', '{1}', '{2}', '{3}');".format(args)
        print(statement)
        cursor.execute(statement)
        #cursor.callproc('my_proc', args)

    except (Error, Warning) as e:
        print e


if __name__ == '__main__':
    conn = connect()
    insert_listen_record(conn, 'aa', 'bb', 'cc', 'dd')
    disconnect(conn) 
输出完全符合您的预期:

connecting...
connected
inserting ['aa', 'bb', 'cc', 'dd'] into the database
CALL my_proc('aa', 'bb', 'cc', 'dd');
connection closed

正如您在过程insert_记录中注释掉的代码中所看到的,我还尝试使用cursor.callproc而不是cursor.execute,但结果相同(缺少)。我不确定我做错了什么,而且由于我对这一切都不熟悉,我甚至不知道从哪里开始调试:存储过程工作正常,没有错误消息,在调试模式下单步执行代码一切似乎都很好…

您可能希望在
语句
字符串的末尾添加一个
COMMIT
语句。我对MySQL知道的不多,但在MSSQL中,我总是在创建/删除一个表等之后发送一个
COMMIT
,以便它实际保留在数据库中。就这么简单吧。。。添加了conn.commit(),它确实做到了这一点。Thx@MathewSavage!