This Code work Properly in MYSQLWorkbench .
use MYDB
drop trigger if exists mytrigger
delimiter //
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;//
delimiter ;
MYTABLE has two column as (COL1,COL2)
I Want to create this trigger via python so i write this code in python:
import mysql.connector
cnx = mysql.connector.connect(user='root', password='root', host='127.0.0.1', database='mysql')
cursor = cnx.cursor()
DB_NAME = 'MYDB'
Table_Name= 'MYTABLE'
cursor.execute("CREATE DATABASE IF NOT EXISTS " + str(DB_NAME))
cnx.commit()
cursor.execute("USE " + str(DB_NAME))
cnx.commit()
cursor.execute("CREATE TABLE IF NOT EXISTS " + str(Table_Name) + " ("
" `COL1` BIGINT(20) NOT NULL ,"
" `COL2` varchar(20) NOT NULL,"
" PRIMARY KEY (`COL1`)"
") ENGINE=InnoDB")
cnx.commit()
cursor.execute(" drop trigger if exists mytrigger")
qrystr = (" delimiter // \n"
" CREATE TRIGGER mytrigger BEFORE INSERT ON MYTABLE FOR EACH ROW \n"
" BEGIN \n"
" IF (select COUNT(*) from MYTABLE) = 12 THEN \n"
" SET NEW.COL2 = 10;\n"
" END IF;\n"
" END;//\n"
" delimiter ; \n")
cursor.execute(qrystr)
cnx.commit()
cnx.close()
BUT This error occur :(((((((((((((((((((((((((((((((
Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'delimiter // CREATE TRIGGER mytrigger BEFORE INSERT ON MYTABLE FOR EACH ROW ' at line 1
please help