你的位置:首页 > 软件开发 > 数据库 > Create function through MySQLdb

Create function through MySQLdb

发布时间:2016-10-16 03:00:06
http://stackoverflow.com/questions/745538/create-function-through-mysqldb How can I define a multi-statement function or procedure in usin ...

http://stackoverflow.com/questions/745538/create-function-through-mysqldb

 

How can I define a multi-statement function or procedure in using the MySQLdb lib in python?

Example:

import MySQLdbdb = MySQLdb.connect(db='service')c = db.cursor()c.execute("""DELIMITER //CREATE FUNCTION trivial_func (radius float)   RETURNS FLOAT  BEGIN  IF radius > 1 THEN    RETURN 0.0;  ELSE    RETURN 1.0;  END IF;END //DELIMITER ;""")

Which creates the following traceback:

Traceback (most recent call last): File "proof.py", line 21, in <module>  DELIMITER ;""") File "build/bdist.macosx-10.5-i386/egg/MySQLdb/cursors.py", line 173, in execute File "build/bdist.macosx-10.5-i386/egg/MySQLdb/connections.py", line 35, in defaulterrorhandler_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 'DELIMITER //\nCREATE FUNCTION trivial_func (radius float) \n  RETURNS FLOAT\n\n  ' at line 1")

If I copy the same SQL directly into a mysql shell client, it works as expected

ʞɔıu23k2070111
up vote15down voteaccepted

原标题:Create function through MySQLdb

关键词:MYSQL

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。