免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5859 | 回复: 2
打印 上一主题 下一主题

用C创建DB2自定义函数遇到的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-10-27 11:55 |只看该作者 |倒序浏览
我想在DB2中用C创建一个自定义的TRIM函数TrimChr.

C代码是在C/C++版上搜到的:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sqludf.h>


//Macro for iendity the blanks
#define isSpaces(ch) (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\b' || ch == '\f' || ch == '\n')

char *TrimChr(char *pszSource)
{
        //pointer of the first char of string
        char *pszHead = pszSource;
        //pointer of the last non-blank char
        char *pszLast = &pszSource[strlen(pszSource)-1];
        //search the location of the last non-blank char
        while (isSpaces(*pszLast))
                --pszLast;
        *(++pszLast) = '\0';
        //search the first non-blank char for left shift the string
        for ( ; (*pszHead != '\0') && (pszHead != pszLast); ++pszHead)
        {
                if (! isSpaces(*pszHead))
                {
                        strcpy(pszSource, pszHead);
                        break;
                }
        }

        return pszSource;
}

int main(int argc,char *argv[])
{
        printf("\"");
        printf(argv[1]);
        printf("\"\n");
        printf("\"");
        printf(TrimChr(argv[1]));
        printf("\"\n");
}

代码可以编译成功并在LINUX环境下运行的.


但是我想用它变成DB2的一个自定义函数时,老是出问题:

values TrimChr('   aaaa    ');

[IBM][CLI Driver][DB2/LINUX] SQL0444N  Routine "TRIMCHR" (specific name "SQL080529112348600") is implemented with code in library or path ".../sqllib/function/TrimChr", function "TrimChr" which cannot be accessed.  Reason code: "5".  SQLSTATE=42724

自定义函数是如此写的:

CREATE FUNCTION TrimChr (VARCHAR(400))
                           RETURNS VARCHAR(400)   
                           EXTERNAL NAME '/home/db2inst1/sqllib/function/TrimChr!TrimChr'
                           LANGUAGE C  
                           NULL CALL  
                           PARAMETER STYLE DB2SQL  
                           NO SQL  
                           DETERMINISTIC  
                           NO EXTERNAL ACTION
                           NOT FENCED;

GRANT EXECUTE ON FUNCTION TRIMCHR(VARCHAR(400)) TO PUBLIC;


请问是什么原因?是编译的问题?还是C函数的问题?

论坛徽章:
0
2 [报告]
发表于 2010-10-27 14:03 |只看该作者
Reason code 5 :
There was insufficient memory to load the library containing
The function or one or more symbols could not be resolved.
Contact the routine creator or your database administrator to
Make sure that the library was correctly linked. All required
Libraries to resolve referenced symbols such as external
Functions must be available. If a lack of memory is determined
Then the system configuration may need to be changed to make more
Memory available to DB2.

论坛徽章:
0
3 [报告]
发表于 2010-11-01 16:33 |只看该作者
问题解决了.

C代码如下:
  1. #include        <string.h>
  2. #include        <memory.h>
  3. #include        <sqludf.h>

  4. #define        ISSPACE(x) ((x)==0x00 ||(x)==0x01 ||(x)==0x02 ||(x)==0x03 ||(x)==0x04 ||(x)==0x05 ||(x)==0x06 ||(x)==0x07 ||(x)==0x08 ||(x)==0x09 ||(x)==0x0A ||(x)==0x0B ||(x)==0x0C ||(x)==0x0D ||(x)==0x0E ||(x)==0x0F ||(x)==0x10 ||(x)==0x11 ||(x)==0x12 ||(x)==0x13 ||(x)==0x14 ||(x)==0x15 ||(x)==0x16 ||(x)==0x17 ||(x)==0x18 ||(x)==0x19 ||(x)==0x1A ||(x)==0x1B ||(x)==0x1C ||(x)==0x1D ||(x)==0x1E ||(x)==0x1F ||(x)==0x20 )

  5. void TrimChr( char *String, char *TrimedString )
  6. {
  7.         char        *Tail, *Head;

  8.         for ( Tail = String + strlen( String ) - 1; Tail >= String; Tail -- )
  9.                 if ( !ISSPACE( *Tail ) )
  10.                         break;
  11.         Tail[1] = 0;
  12.         for ( Head = String; Head <= Tail; Head ++ )
  13.                 if ( !ISSPACE( *Head ) )
  14.                         break;
  15.         if ( Head != String )
  16.                 memcpy( String, Head, ( Tail - Head + 2 ) * sizeof( char ) );
  17.         
  18.         strcpy(TrimedString, String);
  19.         return;
  20. }
复制代码
这段代码是借鉴FH的这个贴子的:
http://bbs.chinaunix.net/viewthread.php?tid=277036

编译命令如下:
  1. cc -c -o TrimChr.o -I ~/sqllib/include TrimChr.c -D_REENTRANT
  2. cc -o TrimChr -shared -fpic TrimChr.o
复制代码
请用db2inst1用户编译,编译成功后,将可执行文件COPY到 ~/sqllib/function/下,即/home/db2inst1/sqllib/function/下.
  1. cp TrimChr ~/sqllib/function/
复制代码
最后到数据库中创建自定义函数:
  1. --DROP FUNCTION TrimChr;

  2. CREATE FUNCTION TrimChr (VARCHAR(400))
  3.                            RETURNS VARCHAR(400)  
  4.                            EXTERNAL NAME '/home/db2inst1/sqllib/function/TrimChr!TrimChr'
  5.                            LANGUAGE C  
  6.                            NULL CALL  
  7.                            PARAMETER STYLE DB2SQL  
  8.                            NO SQL  
  9.                            DETERMINISTIC  
  10.                            NO EXTERNAL ACTION
  11.                            NOT FENCED;

  12. GRANT EXECUTE ON FUNCTION TRIMCHR(VARCHAR(400)) TO PUBLIC;
复制代码
然后就可以直接使用此函数了:
  1. values TrimChr('                 aaa          bbb                          ');
复制代码
本人不懂C,主要是不断的查资料,不断的做实验而成功的.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP