亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 5882 | 回復: 2
打印 上一主題 下一主題

用C創(chuàng)建DB2自定義函數(shù)遇到的問題 [復制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2010-10-27 11:55 |只看該作者 |倒序瀏覽
我想在DB2中用C創(chuàng)建一個自定義的TRIM函數(shù)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環(huán)境下運行的.


但是我想用它變成DB2的一個自定義函數(shù)時,老是出問題:

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

自定義函數(shù)是如此寫的:

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函數(shù)的問題?

論壇徽章:
0
2 [報告]
發(fā)表于 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 [報告]
發(fā)表于 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://www.72891.cn/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用戶編譯,編譯成功后,將可執(zhí)行文件COPY到 ~/sqllib/function/下,即/home/db2inst1/sqllib/function/下.
  1. cp TrimChr ~/sqllib/function/
復制代碼
最后到數(shù)據(jù)庫中創(chuàng)建自定義函數(shù):
  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;
復制代碼
然后就可以直接使用此函數(shù)了:
  1. values TrimChr('                 aaa          bbb                          ');
復制代碼
本人不懂C,主要是不斷的查資料,不斷的做實驗而成功的.
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP