Create function through MySQLdb

Source: Internet
Author: User
Tags function definition mysql query python mysql

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

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

Example:

  import  Mysqldbdb = mysqldb.< Span class= "PLN" >connect (db= ' service ' Span class= "pun" >) c = Db. () c. ( "" "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; "" "               /span>             

which creates the following traceback:

Traceback (Most recent callLast): File "Proof.py",Line21st, Inch <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< Span class= "pun" >/connections.you has 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,409126 Asked Apr at 21:54 ??? U23k111
Add a Comment
3 answersactiveoldestvotes
Up vote15down voteaccepted

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

DELIMITERThe purpose of the avoid ambiguity about CREATE FUNCTION the termination of the statement when the statement Itsel F can contain semicolon characters. This was 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?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 the change of the statement terminator with DELIMITER .

Create function through MySQLdb

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.