星空网 > 软件开发 > 数据库

Create function through MySQLdb

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


python mysql
  
bluish9,4091269126


asked Apr 13 '09 at 21:54
 

ʞɔıu23k2070111


 add a comment

3 Answers

activeoldestvotes



up vote15down voteaccepted

The DELIMITER command is a MySQL shell client builtin, and it's recognized only by that program (and MySQL Query Browser). It's not necessary to use DELIMITER if you execute SQL statements directly through an API.

The purpose of DELIMITER is to help you avoid ambiguity about the termination of the CREATE FUNCTION statement, when the statement itself can contain semicolon characters. This is important in the shell client, where by default a semicolon terminates an SQL statement. You need to set the statement terminator to some other character in order to submit the body of a function (or trigger or procedure).

CREATE FUNCTION trivial_func (radius float)   RETURNS FLOAT  BEGIN  IF radius > 1 THEN    RETURN 0.0; <-- does this semicolon terminate RETURN or CREATE FUNCTION?  ELSE    RETURN 1.0;  END IF;END

Since the API typically allows you to submit one SQL statement at a time, there's no ambiguity -- the interface knows that any semicolons inside the body of your function definition don't terminate the whole CREATE FUNCTION statement. So there's no need to change the statement terminator with DELIMITER.







原标题:Create function through MySQLdb

关键词:MYSQL

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

围观!消费者选择可持续型商品是大势所趋!:https://www.ikjzd.com/articles/135565
 2021年亚马逊新卖家该怎么选品呢?:https://www.ikjzd.com/articles/135567
运营秘籍:Shopee广告成效追踪优化:https://www.ikjzd.com/articles/135571
最后一个月,圣诞、疫情和英国脱欧的三重夹!:https://www.ikjzd.com/articles/135572
OPPO案例分析|建设品牌海外的思想领导力:https://www.ikjzd.com/articles/135573
欧盟初步认定亚马逊破坏公平竞争,或处以巨额罚金:https://www.ikjzd.com/articles/135574
湘西游轮六 湘江游轮夜游:https://www.vstour.cn/a/411226.html
携程旅游网折扣优惠最新攻略(携程旅游网更佳折扣优惠):https://www.vstour.cn/a/411227.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流