Python MySQLdb自定义函数错误

时间:2018-08-05 02:20:34

标签: python mysql-python

我有一个自定义函数,如下所示:

def create_extract_position_type_1(self):

    self.c.execute("""CREATE FUNCTION extract_position_type_1 (f_str VARCHAR)

                        RETURNS VARCHAR

                        BEGIN

                        DECLARE pstr VARCHAR(1600)

                        SET pstr = SUBSTRING(f_str, "|", 4)

                        RETURN pstr

                        END""")

当我运行此函数时,出现此错误:

_mysql_exceptions.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 ')\n\n\t\t\t\t\t\t\tRETURNS VARCHAR\n\n\t\t\t\t\t\t    BEGIN\n\n\t\t\t\t\t\t    DECLARE pstr VARCHAR(1600)' at line 1")

我在做什么错了?

1 个答案:

答案 0 :(得分:0)

SQL语法需要一些更正。 VARCHAR应该有长度限制

尝试一下

DELIMITER $$
CREATE FUNCTION extract_position_type_1(f_str VARCHAR(500) ) RETURNS VARCHAR(500)
BEGIN
    DECLARE pstr VARCHAR(1600);
    SET pstr = SUBSTRING(f_str, '|', 4);
    RETURN pstr;
END