mysql python 中的语法错误
mysql error syntax in python
sql = """
DROP PROCEDURE
IF EXISTS schema_change;
delimiter ';;'
CREATE PROCEDURE schema_change() BEGIN
if exists (select * from information_schema.columns where table_schema =
schema() and table_name = 'selectedairport' and column_name = 'GDP')
then
alter table selectedairport drop column GDP;
alter table selectedairport add column GDP DOUBLE;
end;;
delimiter ';'
CALL schema_change () ; DROP PROCEDURE
IF EXISTS schema_change ;
"""
cursor6.execute(sql)
但是,这会产生错误:
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 ';;'\nCREATE PROCEDURE schema_change() BEGIN\n\n if exists (select * f' at line 1")
可能是什么问题?
execute()
方法(通常)一次只执行一条命令,所以无法解析脚本,反正不支持DELIMITER
;参见 this comment on GitHub。因此,一种解决方案是多次调用:
cursor6.execute("""
DROP PROCEDURE
IF EXISTS schema_change
""")
cursor6.execute("""
CREATE PROCEDURE schema_change() BEGIN
if exists (select * from information_schema.columns where table_schema =
schema() and table_name = 'selectedairport' and column_name = 'GDP')
then
alter table selectedairport drop column GDP;
注意:这里有语法错误,需要进一步补充:
END IF;
现在像以前一样继续:
alter table selectedairport add column GDP DOUBLE;
end
""")
cursor6.execute("""
CALL schema_change ()
""")
# Or cursor6.callproc('schema_change')
cursor6.execute("""
DROP PROCEDURE
IF EXISTS schema_change
""")
sql = """
DROP PROCEDURE
IF EXISTS schema_change;
delimiter ';;'
CREATE PROCEDURE schema_change() BEGIN
if exists (select * from information_schema.columns where table_schema =
schema() and table_name = 'selectedairport' and column_name = 'GDP')
then
alter table selectedairport drop column GDP;
alter table selectedairport add column GDP DOUBLE;
end;;
delimiter ';'
CALL schema_change () ; DROP PROCEDURE
IF EXISTS schema_change ;
"""
cursor6.execute(sql)
但是,这会产生错误:
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 ';;'\nCREATE PROCEDURE schema_change() BEGIN\n\n if exists (select * f' at line 1")
可能是什么问题?
execute()
方法(通常)一次只执行一条命令,所以无法解析脚本,反正不支持DELIMITER
;参见 this comment on GitHub。因此,一种解决方案是多次调用:
cursor6.execute("""
DROP PROCEDURE
IF EXISTS schema_change
""")
cursor6.execute("""
CREATE PROCEDURE schema_change() BEGIN
if exists (select * from information_schema.columns where table_schema =
schema() and table_name = 'selectedairport' and column_name = 'GDP')
then
alter table selectedairport drop column GDP;
注意:这里有语法错误,需要进一步补充:
END IF;
现在像以前一样继续:
alter table selectedairport add column GDP DOUBLE;
end
""")
cursor6.execute("""
CALL schema_change ()
""")
# Or cursor6.callproc('schema_change')
cursor6.execute("""
DROP PROCEDURE
IF EXISTS schema_change
""")