熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> MySQL >> 正文

在Windows上的MySQL UDF開發應用

2013-11-23 20:59:15  來源: MySQL 

  曾以為Windows版本的MySQL存在不能使用UDF的BUG諸提交了一個bug報告不過似乎發現是我搞錯了MySQL的技術支持人員給了非常完美的解答同大家分享一下下邊是原文回復
  
  用戶你好抱歉這並不是一個bug下面我粘貼一個以前為某個客戶做的簡例假設你有了VC++源碼分發並且有一個正常運行的MySQL服務器
  
  我將創建一個UDF它將一個名字:
  
  注意:例子非常簡陋目的是讓你了解該如何處理手頭的UDF
  
   打開 mysqlddsw 工作區
  
   添加新項目到這個工作區
  
   Project name: my_udf // 項目名稱:my_udf
  
   選擇 Win DynamicLink Library // Win動態連接庫
  
   點擊 OK
  
   選擇 An Empty DLL project // 一個空DLL項目
  
   點擊 Finish
  
   點擊 OK
  
   添加新文件 my_udfcpp 到項目中:
  
  #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_udfdef file://文件名:my_udfdef
  
   按照下邊的內容編輯文件
  
  LIBRARY UDF_EXAMPLE
  
  DESCRIPTION Example Using UDF with VC++
  
  VERSION
  
  EXPORTS
  
  my_name
  
   右擊my_udf項目並選擇Settings
  
   點 C/C++ 選項卡
  
   選擇 General
  
   添加宏 HAVE_DLOPE 到預處理器定義
  
   選擇 Preprocessor
  
   添加頭文件路徑: Additional Include directories
  
  例如: /include
  
   按 F 去編譯成 DLL
  
   復制 my_udfdll 到環境變量path定義過的目錄
  
  比如 \winnt\system
  
   打開mysql客戶端
  
  C:\mysqludf\bin>mysql uroot p
  
  Enter password:
  
  Welcome to the MySQL monitor Commands end with ; or \g
  
  Your MySQL connection id is to server version: maxnt
  
  Type help; or \h for help Type \c to clear the buffer
  
  mysql> CREATE FUNCTION my_name RETURNS STRING SONAME my_udfdll;
  
  Query OK rows affected ( sec)
  
  mysql> select my_name();
  
  mysql> drop function my_name;
  
  Query OK rows affected ( sec)
  
  以下是MySQL技術人員的回信原文
  
  Sorry this isnt 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 mysqlddsw workspace
  
   Add New project to the workspace
  
   Project name: my_udf
  
   Select Win DynamicLink Library
  
   Click OK
  
   Select An Empty DLL project
  
   Click Finish
  
   Click OK
  
   Add a new file called my_udfcpp 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_udfdef
  
   Edit the above file with the below contents:
  
  LIBRARY UDF_EXAMPLE
  
  DESCRIPTION Example Using UDF with VC++
  
  VERSION
  
  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
  
  eg: /include
  
   Press F for to build the DLL
  
   Copy the my_udfdll to the environment path directory:
  
  \winnt\system for example
  
   Start the mysql client and issue:
  
  C:\mysqludf\bin>mysql uroot p
  
  Enter password:
  
  Welcome to the MySQL monitor Commands end with ; or \g
  
  Your MySQL connection id is to server version: maxnt
  
  Type help; or \h for help Type \c to clear the buffer
  
  mysql> CREATE FUNCTION my_name RETURNS STRING SONAME my_udfdll;
  
  Query OK rows affected ( sec)
  
  mysql> select my_name();
  
  mysql> drop function my_name;
  
  Query OK rows affected ( sec)
From:http://tw.wingwit.com/Article/program/MySQL/201311/29409.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.