0

Ich benutze sqlalchemy zu Abfrage auf einem MySql-Server von Python ausführen.sqlalchemy Fehler beim Aufruf von mysql gespeicherte Prozedur

ich initialisieren sqlalchemy mit:

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

Wo connection_params einen dict die Server-Zugangsdaten enthalten ist.

Ich bin mit dieser Abfrage:

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; 

Mit conn.execute(query) (wobei i query gleich der Zeichenfolge oben gesetzt).

Das läuft gut.

Ich versuchte, meine Abfrage in einer gespeicherten Prozedur wie gesagt:

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 

ich die gespeicherte Prozedur aus dem Abfrage-Editor in MySQL Workbench mit CALL new_db.test_anag('000000') und ich bekomme das gewünschte Ergebnis (das ist eine einzige Zeile laufen kann).

Jetzt versuche ich zu laufen:

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

Aber es schlägt mit der folgenden Ausnahme

sqlalchemy.exc.InterfaceError: (mysql.connector.errors.InterfaceError) Use multi=True when executing multiple statements [SQL: "CALL projecthf_db.test_anag('0237400')"]

Ich sah mich um, aber ich kann nicht etwas Nützliches zu diesem Fehler finden und für die Liebe Ich kann mich nicht darum kümmern. Ich bin weder auf Mysql noch auf sqlalchemy (oder irgendwas RDBMS) ein Experte, aber dieses sieht so aus, als sollte es leicht zu beheben sein. Lassen Sie mich wissen, wenn mehr Informationen benötigt werden.

Vielen Dank im Voraus für die Hilfe

Antwort

1

Von einem related question Lesen ist zu erkennen, dass mysql.connector automatisch abruft und speichert mehr Ergebnisse, wenn executing stored procedures producing such, auch wenn nur eine Ergebnismenge erzeugt wird. SQLAlchemy auf der anderen Seite does not support multiple result sets – directly. Um gespeicherte Prozeduren auszuführen, verwenden Sie callproc(). Um auf einen DB-API-Cursor in SQLAlchemy zuzugreifen, müssen Sie einen raw connection verwenden. Im Falle von mysql.connector kann auf die produzierten Ergebnissätze zugegriffen werden mit :

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() 
+0

Erstaunliche Antwort, danke für alle Referenzen. Vielen Dank :-) – gionni

Verwandte Themen