当前位置:Gxlcms > 数据库问题 > Create function through MySQLdb

Create function through MySQLdb

时间:2021-07-01 10:21:17 帮助过:10人阅读

Which creates the following traceback:

  1. <code><span class="typ">Traceback<span class="pln"> <span class="pun">(<span class="pln">most recent call <span class="kwd">last<span class="pun">):<span class="pln">
  2. <span class="typ">File<span class="pln"> <span class="str">"proof.py"<span class="pun">,<span class="pln"> line <span class="lit">21<span class="pun">,<span class="pln"> <span class="kwd">in<span class="pln"> <span class="str"><module><span class="pln">
  3. DELIMITER <span class="pun">;<span class="str">""")
  4. File "<span class="pln">build<span class="pun">/<span class="pln">bdist<span class="pun">.<span class="pln">macosx<span class="pun">-<span class="lit">10.5<span class="pun">-<span class="pln">i386<span class="pun">/<span class="pln">egg<span class="pun">/<span class="typ">MySQLdb<span class="pun">/<span class="pln">cursors<span class="pun">.<span class="pln">py<span class="str">", line 173, in execute
  5. File "<span class="pln">build<span class="pun">/<span class="pln">bdist<span class="pun">.<span class="pln">macosx<span class="pun">-<span class="lit">10.5<span class="pun">-<span class="pln">i386<span class="pun">/<span class="pln">egg<span class="pun">/<span class="typ">MySQLdb<span class="pun">/<span class="pln">connections<span class="pun">.<span class="pln">py<span class="str">", line 35, in defaulterrorhandler
  6. _mysql_exceptions.ProgrammingError: (1064, "<span class="typ">You<span class="pln"> have an error <span class="kwd">in<span class="pln"> your SQL syntax<span class="pun">;<span class="pln"> check the manual that corresponds to your <span class="typ">MySQL<span class="pln"> server version <span class="kwd">for<span class="pln"> the right syntax to <span class="kwd">use<span class="pln"> near <span class="str">‘DELIMITER //\nCREATE FUNCTION trivial_func (radius float) \n RETURNS FLOAT\n\n ‘<span class="pln"> at line <span class="lit">1<span class="str">")</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code>

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

python mysql
    bluish 9,4091269126 asked Apr 13 ‘09 at 21:54   ???u 23k2070111
  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).

  1. <code><span class="pln">CREATE FUNCTION trivial_func <span class="pun">(<span class="pln">radius <span class="kwd">float<span class="pun">)<span class="pln">
  2. RETURNS FLOAT
  3. <span class="kwd">BEGIN<span class="pln">
  4. IF radius <span class="pun">><span class="pln"> <span class="lit">1<span class="pln"> THEN
  5. RETURN <span class="lit">0.0<span class="pun">;<span class="pln"> <span class="pun"><--<span class="pln"> does <span class="kwd">this<span class="pln"> semicolon terminate RETURN <span class="kwd">or<span class="pln"> CREATE FUNCTION<span class="pun">?<span class="pln">
  6. ELSE
  7. RETURN <span class="lit">1.0<span class="pun">;<span class="pln">
  8. <span class="kwd">END<span class="pln"> IF<span class="pun">;<span class="pln">
  9. <span class="kwd">END</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code>

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

标签:

人气教程排行