Handling mysql trigger using python
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jay Krishna ReddyRank #152 · 15.2K reputationPosted Jun 30, 2022, 4:53 AM EDT
For anyone else coming along with this problem (or on the off-chance that this is still an issue for the asker nearly 11 months later):
DELIMITER is a feature only of the CLI (not the server itself), so the various connectors won't know what to do with it. I ended up solving my same problem by removing the DELIMITER statement and anything that relied on it and moving the entire trigger statement onto one line to execute at once. Because it's all in one execution, the connector determines that the semicolons don't end the statement.
cursor.execute(" drop trigger if exists mytrigger")
## NEW CODE ##
qrystr = "CREATE TRIGGER mytrigger BEFORE INSERT ON MYTABLE FOR EACH ROW BEGIN IF (select COUNT(*) from MYTABLE) = 12 THEN SET NEW.COL2 = 10; END IF; END"
##############
cursor.execute(qrystr)
cnx.commit()
cnx.close()
Hope this helps you
Md SarfarajRank #194 · 12.4K reputationPosted Jul 1, 2022, 2:43 AM EDT