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 |
DELIMITER the 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.
DELIMITER The 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