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 MySQLdb

db = 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

 

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).

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.

时间: 2024-11-29 07:30:16

Create function through MySQLdb的相关文章

Create Function

示例,创建一个名为HelloWorld4的函数,不需要输入参数 CREATE FUNCTION HelloWorld4()RETURNS VARCHAR(20)ASBEGINRETURN 'Hello World!';END select dbo.helloworld4() =================================

Sql Create Function简单例子

create function test(@Num varchar(20))--@Num 参数 returns varchar(50) --返回值类型 as begin declare @MSG varchar(20) if(@Num =1) select @MSG ='正确' else select @MSG ='错误' return @MSG end --调用函数select dbo.test(2)--传递参数2返回结果:错误 --创建返回Table类型的函数 create function

MySQL 自定义函数CREATE FUNCTION实例

分享一个MySQL 自定义函数CREATE FUNCTION的实例.mysql> delimiter $$mysql> CREATE FUNCTION myFunction-> (in_string VARCHAR(255),-> in_find_str VARCHAR(20),-> in_repl_str VARCHAR(20))->-> RETURNS VARCHAR(255)-> BEGIN-> DECLARE l_new_string VARC

sql:MySql create FUNCTION,VIEW,PROCEDURE

use geovindu; #函数 DELIMITER $$ drop function if exists f_GetDepartmentName $$ CREATE function f_GetDepartmentName ( did int ) returns nvarchar(400) begin declare str nvarchar(100); select DepartmentName into str from DepartmentList where DepartmentID

CREATE FUNCTION - 定义一个新函数

SYNOPSIS CREATE [ OR REPLACE ] FUNCTION name ( [ argtype [, ...] ] ) RETURNS rettype { LANGUAGE langname | IMMUTABLE | STABLE | VOLATILE | CALLED ON NULL INPUT | RETURNS NULL ON NULL INPUT | STRICT | [EXTERNAL] SECURITY INVOKER | [EXTERNAL] SECURITY

mysql create function error and solution way.

Error Code: 1418. This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled (you *might* want to use the less safe log_bin_trust_function_creators variable reason : mysql的设置默认是不允许创建函数 解决办法1: 执

[Hive - LanguageManual] Create/Drop/Alter View Create/Drop/Alter Index Create/Drop Function

Create/Drop/Alter View Create View Drop View Alter View Properties Alter View As Select Version information Icon View support is only available in Hive 0.6 and later. Create View CREATE VIEW [IF NOT EXISTS] view_name [(column_name [COMMENT column_com

Adding New Functions to MySQL(User-Defined Function Interface UDF、Native Function)

catalog 1. How to Add New Functions to MySQL 2. Features of the User-Defined Function Interface 3. User-Defined Function 4. UDF Argument Processing 5. UDF Return Values and Error Handling 6. UDF Compiling and Installing 7. Adding a New Native Functio

前端开发者进阶之ECMAScript新特性--Object.create

前端开发者进阶之ECMAScript新特性[一]--Object.create Object.create(prototype, descriptors) :创建一个具有指定原型且可选择性地包含指定属性的对象 参数:prototype 必需.  要用作原型的对象. 可以为 null.descriptors 可选. 包含一个或多个属性描述符的 JavaScript 对象."数据属性"是可获取且可设置值的属性. 数据属性描述符包含 value 特性,以及 writable.enumerab