【问题标题】:sqlalchemy error when calling mysql stored procedure调用mysql存储过程时出现sqlalchemy错误
【发布时间】:2018-02-09 07:42:33
【问题描述】:

我正在使用 sqlalchemy 从 python 在 MySql 服务器上运行查询。

我使用以下方法初始化 sqlalchemy:

engine = create_engine("mysql+mysqlconnector://{user}:{password}@{host}:{port}/{database}".format(**connection_params))
conn = engine.connect()

其中connection_params 是一个包含服务器访问详细信息的字典。

我正在运行这个查询:

SELECT 
new_db.asset_specification.identifier_code, 
new_db.asset_specification.asset_name, 
new_db.asset_specification.asset_type, 
new_db.asset_specification.currency_code, 
new_db.sector_map.sector_description, 
new_db.super_sector_map.super_sector_description, 
new_db.country_map.country_description, 
new_db.country_map.country_macro_area 

FROM new_db.asset_specification 
INNER JOIN new_db.identifier_code_legal_entity_map on new_db.asset_specification.identifier_code = new_db.identifier_code_legal_entity_map.identifier_code 
INNER JOIN new_db.legal_entity_map on projecthf_db.identifier_code_legal_entity_map.legal_entity_code = new_db.legal_entity_map.legal_entity_code 
INNER JOIN new_db.sector_map on new_db.legal_entity_map.legal_entity_sector = new_db.sector_map.sector_code 
INNER JOIN new_db.super_sector_map on projecthf_db.legal_entity_map.legal_entity_super_sector = new_db.super_sector_map.super_sector_code 
INNER JOIN new_db.country_map on new_db.legal_entity_map.legal_entity_country = new_db.country_map.country_code 
WHERE new_db.asset_specification.identifier_code = str_identifier_code;

使用conn.execute(query)(我设置query 等于上面的字符串)。

这运行得很好。

我试图将我的查询放在一个存储过程中,例如:

CREATE DEFINER=`root`@`localhost` PROCEDURE `test_anag`(IN str_identifier_code varchar(100))
BEGIN
SELECT 
new_db.asset_specification.identifier_code, 
new_db.asset_specification.asset_name, 
new_db.asset_specification.asset_type, 
new_db.asset_specification.currency_code, 
new_db.sector_map.sector_description, 
new_db.super_sector_map.super_sector_description, 
new_db.country_map.country_description, 
new_db.country_map.country_macro_area 

FROM new_db.asset_specification 
INNER JOIN new_db.identifier_code_legal_entity_map on new_db.asset_specification.identifier_code = new_db.identifier_code_legal_entity_map.identifier_code 
INNER JOIN new_db.legal_entity_map on projecthf_db.identifier_code_legal_entity_map.legal_entity_code = new_db.legal_entity_map.legal_entity_code 
INNER JOIN new_db.sector_map on new_db.legal_entity_map.legal_entity_sector = new_db.sector_map.sector_code 
INNER JOIN new_db.super_sector_map on projecthf_db.legal_entity_map.legal_entity_super_sector = new_db.super_sector_map.super_sector_code 
INNER JOIN new_db.country_map on new_db.legal_entity_map.legal_entity_country = new_db.country_map.country_code 
WHERE new_db.asset_specification.identifier_code = str_identifier_code;

END

我可以使用CALL new_db.test_anag('000000')从mysql工作台中的查询编辑器运行存储过程,我得到了想要的结果(这是一行)。

现在我尝试运行:

res = conn.execute("CALL new_db.test_anag('000000')")

但它失败并出现以下异常

sqlalchemy.exc.InterfaceError: (mysql.connector.errors.InterfaceError) 执行多条语句时使用multi=True [SQL: "CALL projecthf_db.test_anag('0237400')"]

我环顾四周,但找不到任何有用的信息来解决这个错误,而且为了我的爱,我无法理解它。我不是 Mysql 和 sqlalchemy(或任何 RDBMS)方面的专家,但这个看起来应该很容易修复。如果需要更多信息,请告诉我。

提前感谢您的帮助

【问题讨论】:

    标签: python mysql stored-procedures sqlalchemy mysql-connector-python


    【解决方案1】:

    通过读取related question可以看出mysql.connectorexecuting stored procedures producing such时自动获取并存储多个结果集,即使只产生一个结果集。另一方面,SQLAlchemy does not support multiple result sets – directly。要执行存储过程,请使用callproc()。要在 SQLAlchemy 中访问 DB-API 游标,您必须使用 raw connection。在 mysql.connector 的情况下,可以使用 stored_results() 访问生成的结果集:

    from contextlib import closing
    
    # Create a raw MySQLConnection
    conn = engine.raw_connection()
    
    try:
        # Get a MySQLCursor
        with closing(conn.cursor()) as cursor:
            # Call the stored procedure
            result_args = cursor.callproc('new_db.test_anag', ['000000'])
            # Iterate through the result sets produced by the procedure
            for result in cursor.stored_results():
                result.fetchall()
    
    finally:
        conn.close()
    

    【讨论】:

    • 惊人的答案,感谢所有参考。非常感谢:-)
    猜你喜欢
    • 2018-11-13
    • 1970-01-01
    • 2013-10-23
    • 1970-01-01
    • 2015-08-16
    • 1970-01-01
    • 2022-10-07
    • 2012-12-11
    • 2020-02-15
    相关资源
    最近更新 更多