2

I have a stored procedure.

calling it via MySQL workbench as follows working;

CALL `lobdcapi`.`escalatelobalarm`('A0001');

But not from the python program. (means it is not throwing any exception, process finish execution silently) if I make any error in column names, then at python I get an error. So it calls my stored procedure but not working as expected. (it is an update query .it needs SAFE update )

Why through the python sqlalchemy this update didn't update any records?

CREATE DEFINER=`lob`@`%` PROCEDURE `escalatelobalarm`(IN client_id varchar(50))
BEGIN

 SET SQL_SAFE_UPDATES = 0;                                   
update lobdcapi.alarms
    set lobalarmescalated=1
where id in (

    SELECT al.id 
    from (select id,alarmoccurredhistoryid from lobdcapi.alarms where lobalarmpriorityid=1 and lobalarmescalated=0 and clientid=client_id 
            and alarmstatenumber='02' ) as al
    inner join lobdcapi.`alarmhistory` as hi on hi.id=al.alarmoccurredhistoryid
            and hi.datetimestamp<=  current_timestamp() )

);

SET SQL_SAFE_UPDATES = 1;

END

I call it like;

from sqlalchemy import and_, func,text


db.session.execute(text("CALL escalatelobalarm(:param)"), {'param': clientid})

I suspect the param I pass via code didn't get bind properly?

Dids
  • 137
  • 2
  • 2
  • 18
Ratha
  • 9,434
  • 17
  • 85
  • 163
  • Does this answer your question? [stored procedures with sqlAlchemy](https://stackoverflow.com/questions/3563738/stored-procedures-with-sqlalchemy) – Yugandhar Chaudhari Nov 22 '19 at 05:48
  • 1
    @YugandharChaudhari i got answer from Krik. It is simple, I haven't used session.commit that was the issue – Ratha Nov 22 '19 at 05:53

1 Answers1

3

I haven't called stored procs from SQLAlchemy, but it seems possible that this could be within a transaction because you're using the session. Perhaps calling db.session.commit() at the end would help?

If that fails, SQLAlchemy calls out calling stored procs here. Perhaps try their method of using callproc. Adapting to your use-case, something like:

connection = db.session.connection()
try:
    cursor = connection.cursor()
    cursor.callproc("escalatelobalarm", [clientid])
    results = list(cursor.fetchall())
    cursor.close()
    connection.commit()
finally:
    connection.close()
Kirk
  • 1,779
  • 14
  • 20