热烈祝贺台州朗动科技的站长论坛隆重上线!(2012-05-28)    热烈庆祝伟大的祖国60周年生日 点击进来我们一起为她祝福吧(2009-09-26)    站长论坛禁止发布广告,一经发现立即删除。谢谢各位合作!.(2009-08-08)    热烈祝贺台州网址导航全面升级,全新版本上线!希望各位一如既往地支持台州网址导航的发展.(2009-03-28)    台州站长论坛恭祝各位新年快乐,牛年行大运!(2009-01-24)    台州Link正式更名为台州网址导航,专业做以台州网址为主的网址导航!(2008-05-23)    热烈祝贺台州Link资讯改名为中国站长资讯!希望在以后日子里得到大家的大力支持和帮助!(2008-04-10)    热烈祝贺台州Link论坛改名为台州站长论坛!希望大家继续支持和鼓励!(2008-04-10)    台州站长论坛原[社会琐碎]版块更名为[生活百科]版块!(2007-09-05)    特此通知:新台州站长论坛的数据信息全部升级成功!">特此通知:新台州站长论坛的数据信息全部升级成功!(2007-09-01)    台州站长论坛对未通过验证的会员进行合理的清除,请您谅解(2007-08-30)    台州网址导航|上网导航诚邀世界各地的网站友情链接和友谊联盟,共同引领网站导航、前进!(2007-08-30)    禁止发广告之类的帖,已发现立即删除!(2007-08-30)    希望各位上传与下载有用资源和最新信息(2007-08-30)    热烈祝贺台州站长论坛全面升级成功,全新上线!(2007-08-30)    
便民网址导航,轻松网上冲浪。
台州维博网络专业开发网站门户平台系统
您当前的位置: 首页 » MySQL/MSSQL编程 » 在Windows上的MySQL UDF开发应用

在Windows上的MySQL UDF开发应用

论坛链接
  • 在Windows上的MySQL UDF开发应用
  • 发布时间:2008-11-03 15:42:33    浏览数:4940    发布者:ajax    设置字体【   
曾以为Windows版本的MySQL存在不能使用UDF的BUG诸提交了一个bug报告。不过似乎发现是我搞错了,MySQL的技术支持人员给了非常完美的解答,同大家分享一下。下边是原文回复。
  
用户你好。抱歉,这并不是一个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
  
  #include
  
  #include
  
  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 'Example Using UDF with VC++'
  
  VERSION 1.0
  
  EXPORTS
  
  my_name
  
  - 右击my_udf项目并选择Settings
  
  - 点 C/C++ 选项卡
  
  - 选择 General
  
  - 添加宏 HAVE_DLOPE 到预处理器定义
  
  - 选择 Preprocessor
  
  - 添加头文件路径: Additional Include directories
  
  例如: ../include
  
  - 按 F7 去编译成 DLL.
  
  - 复制 my_udf.dll 到环境变量path定义过的目录
  
  比如 \winnt\system32 。
  
  - 打开mysql客户端
  
  C:\mysql-udf\bin>mysql -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)

  
以下是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
  
  #include
  
  #include
  
  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.
  
  - Copy the my_udf.dll to the environment path directory:
  
  \winnt\system32 for example.
  
  - Start the mysql client and issue:
  
  C:\mysql-udf\bin>mysql -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)
娱乐休闲专区A 影视预告B 音乐咖啡C 英语阶梯D 生活百科
网页编程专区E AMPZF HTMLG CSSH JSI ASPJ PHPK JSPL MySQLM AJAX
Linux技术区 N 系统管理O 服务器架设P 网络/硬件Q 编程序开发R 内核/嵌入
管理中心专区S 发布网址T 版主议事U 事务处理