我正在使用sqlalchemy从python在MySql服务器上运行查询。sqlalchemy在调用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
我可以从MySQL工作台查询编辑器与CALL new_db.test_anag('000000')
运行存储过程,我得到了想要的结果(这是一个单行)。
现在,我尝试运行:
res = conn.execute("CALL new_db.test_anag('000000')")
但它失败,出现以下异常
sqlalchemy.exc.InterfaceError: (mysql.connector.errors.InterfaceError) Use multi=True when executing multiple statements [SQL: "CALL projecthf_db.test_anag('0237400')"]
我环顾四周,但我找不到任何关于此错误的有用和爱我无法绕过它。我不是Mysql或sqlalchemy(或任何RDBMS)的专家,但这看起来应该很容易修复。让我知道是否需要更多信息。
提前感谢您的帮助
令人惊叹的答案,感谢所有参考。非常感谢你 :-) – gionni