Image、Audio 及 Video Extenders 管理與程式設計

範例

下列範例中,會使用預設表格空間,讓資料庫來保留影像資料。

使用 API: 下圖中的程式碼在啟用之前,會先連接到現存的資料庫。此範例是以 DB2 CLI (DB2 call level interface) 來撰寫。包括一些設置和錯誤檢查碼。完整範例程式位於 SAMPLES 次目錄中的 ENABLE.C 檔。

圖 10. 啟用資料庫的範例程式碼

 
/*---- Set-up -------------------------------------------------*/
#include <stdio.h>
#include <stlib.h>
#include <string.h>
#include "dmbimage.h"       /* image extender function prototypes (DBi) */
#include "utility.h"        /* utility functions */
 
#define MMDB_ERROR_MSG_TEXT_LEN         1000
#define SERVER_IS_DB2390  (strcmp(dbms, "DB2")==0 || strcmp(dmbs, "DSN06010")==0)
 
int
main(int argc, char *argv[])
{
        SQLHENV henv = SQL_NULL_HENV;
        SQLHDBC hdbc = SQL_NULL_HDBC;
        SQLHSTMT hstmt = SQL_NULL_HSTMT;
SQLCHAR uid[18+1];
SQLCHAR pwd[30+1];
        SQLCHAR dbname[SQL_MAX_DSN_LENGTH+1];
        SQLCHAR buffer[500];
        SQL SMALLINT dbms_sz = 0;
        char    dbms[20];
 
        SQLRETURN rc = SQL_SUCCESS;
        SQLINTEGER sqlcode = 0;
        char errorMsgText[MMDB_ERROR_MSG_TEXT_LEN+1];
        char *program = "enable;
        char *step;
         
 
/*---- Prompt for database name, userid, and password ----*/
        if (argc > 5) ||  (argc >=2 && strcmp(argv[1],"?")== 0))
          {
            printf("Syntax for enable - enabling a DB2 UDB database: \n"
                   "   enable database_name userid password\n");
      exit(0);
          }
 
        if (argc == 4) {
          strcpy((char *)dbname, argv[1]);
          strcpy((char *)uid   , argv[2]);
          strcpy((char *)pwd   , argv[3]);
          }
  else {
          printf("Enter database name:\n");
  gets((char *) dbName);
          printf("Enter userid:\n");
  gets((char *) uid); 
          printf("Enter password:\n");
  gets((char *) pwd);
          }
/*----- connect to the database -----------------------------------------------*/
         rc = cliInitialize(&henv, &hdbc, dbname, uid, pwd);
cliCheckError(henv, hdbc, SQL_NULL_HSTMT, rc);
         if (rc < 0) goto SERROR;
 
/*----- find out if application is connected to DB2/UDB or DB2/390?------------*/
         rc = SQLGetInfo(hdbc, SQL_DBMS_NAME, (SQLPOINTER) &dbms,
                         sizeof(dbms), &dbms_sz);
cliCheckError(henv, hdbc, SQL_NULL_HSTMT, rc); 
         if (rc < 0) goto SERROR;
  
/****** enable server for image extender ****************************************/
     if (!SERVER_IS_DB2390)
           {
            printf("%s: Enabling database......\n", program);
           }
         printf("%s: This may take a few minutes, please wait......\n", program);
 
     if (!SERVER_IS_DB2390)
           {
            step="DBiEnableDatabase with NULL tablespace"
            rc=DBiEnableDatabase(NULL);
           }
if (rc < 0) {
                printf("%s: %s failed!\n", program, step);
   printMsg(rc);
        DBiGetError(&sqlcode, errorMsgText);
           if (sqlcode)
              printf("sqlcode=%i, ",sqlcode);
} else if (rc > 0) {
           printf("%s: %s, warning detected.\n", program, step);
   printMsg(rc);
        DBiGetError(&sqlcode, errorMsgText);
            printf("warning MsgText=%s\n", errorMsgText);
     } else
           printf("%s: %s OK\n", program, step);
/****** end of enable server *************************************************/
 

使用 db2ext 指令行: 在本例中, 已連接資料庫。

enable database for db2image


[ 頁面頂端 | 前一頁 | 下一頁 | 目錄 | 索引 ]