简体   繁体   English

python中的mysql错误语法

[英]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)

However, this produces the error: 但是,这会产生错误:

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") pymysql.err.ProgrammingError:(1064,“您的SQL语法有错误;请查看与您的MySQL服务器版本相对应的手册,以在'delimiter'附近使用正确的语法;;'\\ n创建过程schema_change()开始n \\ n如果存在(在第1行选择* f')

What could be the problem? 可能是什么问题呢?

The execute() method (usually) only executes a single command at a time, so the script cannot be parsed, and anyway there is no support for DELIMITER ; execute()方法(通常)一次只执行一个命令,因此无法解析脚本,并且无论如何不支持DELIMITER see this comment on GitHub . 在GitHub上查看此评论 Therefore, one solution is to have multiple calls: 因此,一种解决方案是进行多个调用:

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;

NOTE : There is a syntax error here, we need to further add: 注意 :此处存在语法错误,我们需要进一步添加:

    END IF;

Now continue as before: 现在像以前一样继续:

    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
""")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM