Python 调用mysql存储过程时发生sqlalchemy错误
我正在使用sqlalchemy从python在MySql服务器上运行查询 我用以下方法初始化sqlalchemy:Python 调用mysql存储过程时发生sqlalchemy错误,python,mysql,stored-procedures,sqlalchemy,mysql-connector-python,Python,Mysql,Stored Procedures,Sqlalchemy,Mysql Connector Python,我正在使用sqlalchemy从python在MySql服务器上运行查询 我用以下方法初始化sqlalchemy: engine = create_engine("mysql+mysqlconnector://{user}:{password}@{host}:{port}/{database}".format(**connection_params)) conn = engine.connect() 其中,connection\u params是包含服务器访问详细信息的dict 我正在运行此查
engine = create_engine("mysql+mysqlconnector://{user}:{password}@{host}:{port}/{database}".format(**connection_params))
conn = engine.connect()
其中,connection\u params
是包含服务器访问详细信息的dict
我正在运行此查询:
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 workbench中的查询编辑器中使用调用new\u db.test\u anag('000000')
运行存储过程,并得到所需的结果(这是一行)
现在我尝试运行:
res = conn.execute("CALL new_db.test_anag('000000')")
但它失败了,只有以下例外
sqlalchemy.exc.InterfaceError:(mysql.connector.errors.InterfaceError)在执行多个语句时使用multi=True[SQL:“调用projecthf\u db.test\u anag('0237400')”]
我环顾四周,但在这个错误上我找不到任何有用的东西,看在我的份上,我的脑袋也绕不开它。我不是Mysql或sqlalchemy(或任何RDBMS)方面的专家,但这一个看起来应该很容易修复。如果需要更多信息,请告诉我
提前感谢阅读a提供的帮助,可以看到,即使只生成一个结果集,也会自动获取和存储多个结果集。另一方面是炼金术。要执行存储过程,请使用。要在SQLAlchemy中访问DB-API游标,必须使用。对于mysql.connector,可以使用以下方法访问生成的结果集:
惊人的答案,谢谢所有的参考资料。非常感谢:-)
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()