I'm trying to run an sql commands with sqlalchemy in python, all the queries I'm trying to do are running successfully besides one, and the error I get is about syntax. when I take the code the an sql environment it works successfully. Here is the code I'm trying to run (this part is working!):
con.execute("USE covid19_korea")
con.execute("SET @currdate := (select min(confirmed_date) from facts)")
con.execute("SET @enddate := (select DATE_ADD((select max(confirmed_date) from facts), INTERVAL 14 DAY))")
From this part it doesn't work:
query = """
delimiter $$
DROP PROCEDURE IF EXISTS BuildDate$$
CREATE PROCEDURE BuildDate()
BEGIN
WHILE @currdate < @enddate DO
INSERT INTO Dimension_Date (date, day, month, year, day_name)
VALUES (
@currdate, DAY(@currdate), MONTH(@currdate), YEAR(@currdate), DAYNAME(@currdate)
);
SET @currdate:= DATE_ADD(@currdate, INTERVAL 1 DAY);
END WHILE;
END$$
CALL BuildDate();"""
con.execute(query)
and here is the error I get:
ProgrammingError: (pymysql.err.ProgrammingError) (1064, **"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 $$\n DROP PROCEDURE IF EXISTS BuildDate$$\n CREATE PROCEDU' at line 1")**
[SQL:
delimiter $$
DROP PROCEDURE IF EXISTS BuildDate$$
CREATE PROCEDURE BuildDate()
BEGIN
WHILE @currdate < @enddate DO
INSERT INTO Dimension_Date (date, day, month, year, day_name)
VALUES (
@currdate, DAY(@currdate), MONTH(@currdate), YEAR(@currdate), DAYNAME(@currdate)
);
SET @currdate:= DATE_ADD(@currdate, INTERVAL 1 DAY);
END WHILE;
END$$
CALL BuildDate();]
(Background on this error at: http://sqlalche.me/e/f405)