Windows上的MySQL UDF开发

2016-01-29 16:19 15 1 收藏

Windows上的MySQL UDF开发,Windows上的MySQL UDF开发

【 tulaoshi.com - SQLServer 】

 

曾以为Windows版本的MySQL存在不能使用UDF的BUG诸提交了一个bug报告。不过
似乎发现是我搞错了,MySQL的技术支持人员给了非常完美的解答,同大家分享
一下。下边是原文回复 :)

Sorry this isn't a bug.
Below I pasted a sample I did sometime ago for another user:

Ok. Assuming you have VC++ and the source distribution and a server
running,
I will create a UDF that returns a name:

Note: the sample is ugly, but the purpose here is to show you how
to handle the UDF.

- Open the mysqld.dsw workspace.
- Add New project to the workspace
- Project name: my_udf
- Select Win32 Dynamic-Link Library
- Click OK
- Select An Empty DLL project
- Click Finish
- Click OK
- Add a new file called my_udf.cpp to the project:

#include <stdlib.h
#include <winsock.h
#include <mysql.h

extern "C" {
char *my_name(UDF_INIT *initid, UDF_ARGS *args, char *is_null,
       char *error);
}

char *my_name(UDF_INIT *initid, UDF_ARGS *args, char *is_null,
       char *error)
{
    char * me = "my name";

    return me;
}

- Type Ctrl+N for to create a new file.
- Select text type
- File name: my_udf.def
- Edit the above file with the below contents:
LIBRARY         UDF_EXAMPLE
DESCRIPTION             'Example Using UDF with VC++'
VERSION         1.0
EXPORTS
        my_name

- Right Click the my_udf project and select Settings
- Click the C/C++ tab
- Select General in the Category Combo
- Add the macro HAVE_DLOPEN to the PreProcessor definition
- Select Preprocessor in the Category Combo
- Add the include path to the text box: Additional Include directories
  e.g: ../include
- Press F7 for to build the DLL.

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/sqlserver/)

- Copy the my_udf.dll to the environment path directory:
  winntsystem32 for example.

- Start the mysql client and issue:

C:mysql-udfbinmysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 2 to server version: 3.23.52-max-nt

Type 'help;' or 'h' for help. Type 'c' to clear the buffer.

mysql CREATE FUNCTION my_name RETURNS STRING SONAME "my_udf.dll";
Query OK, 0 rows affected (0.08 sec)

mysql select my_name();

mysql drop function my_name;
Query OK, 0 rows affected (0.00 sec)

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/sqlserver/)

画蛇添足的作下简要中文说明。

抱歉,这并不是一个bug。下面我粘贴一个以前为某个客户做的简例,假设你有了
VC++,源码分发,并且有一个正常运行的MySQL服务器。

我将创建一个UDF它将一个名字:
注意:例子非常简陋,目的是让你了解该如何处理手头的UDF。

- 打开 mysqld.dsw 工作区。
- 添加新项目到这个工作区
- Project name: my_udf            // 项目名称:my_udf
- 选择 Win32 Dynamic-Link Library // Win32动态连接库
- 点击 OK
- 选择 An Empty DLL project       // 一个空DLL项目
- 点击 Finish
- 点击 OK
- 添加新文件 my_udf.cpp 到项目中:
#include <stdlib.h
#include <winsock.h
#include <mysql.h

extern "C" {
char *my_name(UDF_INIT *initid, UDF_ARGS *args, char *is_null,
       char *error);
// 兼容C
}

char *my_name(UDF_INIT *initid, UDF_ARGS *args, char *is_null,
       char *error)
{
    char * me = "my name";

    return me;
    // 调用此UDF将返回 my name
}
- 按 Ctrl+N 来创建一个新文件。
- 选择 text 类型
- File name: my_udf.def file://文件名:my_udf.def
- 按照下边的内容编辑文件。
LIBRARY         UDF_EXAMPLE
DESCRIPTION     

来源:https://www.tulaoshi.com/n/20160129/1498261.html

延伸阅读
谈软件技术不可能不说到伟大的软件巨人:微软,不可能不谈windows--垄断了这个星球的pc的操作系统,windows下开发技术一丝一毫的变化都引人注目。 微软在今年的tech.ed大会上正式公布了longhorn研发计划,我们不得不等到2006年才能一览它的风采,但微软同时宣布将longhorn开发平台WinFX中的显示子系统Avalon和通讯子系统Indigo移植到wi...
标签: PHP
来源:www.freelamp.com LAMP 架构的网站,我以前注重的多是安装/配置方面的,讲述开发的相对较少,因为自己从事开发也少。本文的原文当然也来自: Published on The O'Reilly Network (http://www.oreillynet.com/) http://www.oreillynet.com/pub/a/onlamp/2002/04/04/webdb.html 看了以后,颇有启发,以前开发中遇到...
标签: PHP
  <?  LAMP 架构的网站,我以前注重的多是安装/配置方面的,讲述开发的相对较少,因为自己从事开发也少。本文的原文当然也来自:    Published on The O'Reilly Network (http://www.oreillynet.com/)  http://www.oreillynet.com/p...
标签: MySQL mysql数据库
  以下就是针对MySQL作为专门的数据库服务器的优化建议: MySQL服务器的规划 为了以后维护,升级备份的方便和数据的安全性,最好将MySQL程序文件...
LAMP 架构的网站,我以前注重的多是安装/配置方面的,讲述开发的相对较少,因为自己从事开发也少。本文的原文当然也来自: Published on The O'Reilly Network (http://www.oreillynet.com/) http://www.oreillynet.com/pub/a/onlamp/2002/04/04/webdb.html 看了以后,颇有启发,以前开发中遇到的一些问题,迎刃而解。所以翻译出来和...

经验教程

462

收藏

46
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部