我正在尝试在python中使用sqlalchemy运行sql命令,我正在尝试执行的所有查询都成功运行了,而我得到的错误是关于语法的。当我在sql环境中获取代码时,它可以成功运行。这是我要运行的代码(此部分正在运行!):
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))")
这部分无效:
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)
这是我得到的错误:
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)