ページの先頭行へ戻る
Big Data Integration ServerV1.4.0 検索編アプリケーション開発ガイド
FUJITSU Software

G.2.1 データを追加する

データを追加する場合のC APIの使用例を示します。


APIの使用例


以下にC APIを使用したプログラミング例を示します。

#include <stdio.h>
#include <string.h>

#include "libshun.h"

/* データを追加します */
int main()
{
  /* ハンドル変数群 */
  SHUNHCON  connectionHandle;
  SHUNHSTMT statementHandle;

  /* 作業用変数群 */
  int status;
  
  /* 入力パラメタ群 */
  char *hostName;
  int portNo;
  char *ShunFileName;
  
  SHUNDATA inputData[1];
  
  /* エラー用変数群 */
  SHUNHANDLE errorHandle;
  int errorLevel;
  char *errorMessage;


  /* 変数の初期化 */
  connectionHandle = NULL;
  statementHandle = NULL;

  /* コネクションハンドルの割当て */
  status = ShunAllocHandle( NULL, &connectionHandle );
  if ( status != SHUN_SUCCESS ) {
    errorHandle = (SHUNHANDLE)connectionHandle;
    goto ErrorEnd;
  }

  /* ホスト名・ポート番号、Shunsakuファイル名を指定してコネクションを確立 */
  hostName = "DirSvr1";
  portNo = 23101;
  ShunFileName = "shunsakuFile1";
  status = ShunConnect( connectionHandle, hostName, portNo, ShunFileName );
  if ( status != SHUN_SUCCESS ) {
    errorHandle = (SHUNHANDLE)connectionHandle;
    goto ErrorEnd;
  }

  /* データ操作ハンドルの割当て */
  status = ShunAllocHandle( connectionHandle, &statementHandle );
  if ( status != SHUN_SUCCESS ) {
    errorHandle = (SHUNHANDLE)connectionHandle;
    goto ErrorEnd;
  }

  /* 追加するデータの作成 */
  inputData[0].Data =
    "<document>"
    "    <base>"
    "        <name>ホテル9</name>"
    "        <prefecture>神奈川</prefecture>"
    "        <address>神奈川県横浜市神奈川区</address>"
    "        <detail>http://xxxxx.co.jp</detail>"
    "        <price>6000</price>"
    "    </base>"
    "    <information>"
    "        <date>2006年07月18日</date>"
    "    </information>"
    "    <note>バス付 トイレ付 地下鉄 △△駅徒歩05分</note>"
    "</document>";
  inputData[0].Data_Len = strlen( inputData[0].Data );

  /* API呼出し データ追加 */
  status = ShunInsert(
    statementHandle,
    1,
    inputData );
  if ( status != SHUN_SUCCESS ) {
    errorHandle = (SHUNHANDLE)statementHandle;
    goto ErrorEnd;
  }
  
  printf( "追加成功\n" );
  
  /* データ操作ハンドルの解放 */
  status = ShunFreeHandle( statementHandle );
  if ( status != SHUN_SUCCESS ) {
    errorHandle = (SHUNHANDLE)statementHandle;
    goto ErrorEnd;
  }

  /* コネクションの切断 */
  status = ShunDisconnect( connectionHandle );
  if ( status != SHUN_SUCCESS ) {
    errorHandle = (SHUNHANDLE)connectionHandle;
    goto ErrorEnd;
  }

  /* コネクションハンドルの解放 */
  status = ShunFreeHandle( connectionHandle );
  if ( status != SHUN_SUCCESS ) {
    errorHandle = (SHUNHANDLE)connectionHandle;
    goto ErrorEnd;
  }

  return 0;



ErrorEnd: /* エラーが発生した際のエラー処理 */

  /* エラー情報の取得 */
  status = ShunGetErrorMessage(errorHandle,
                               &errorLevel, &errorMessage);

  if ( status == SHUN_SUCCESS ) {
    switch ( errorLevel ) {
    case SHUN_ERROR_CONNECTION:
      printf("エラーレベル              = SHUN_ERROR_CONNECTION\n");
      break;
    case SHUN_ERROR_DATA:
      printf("エラーレベル              = SHUN_ERROR_DATA\n");
      break;
    }
    printf("エラーメッセージ          = %s\n", errorMessage);
  }
  else {
    printf("エラーメッセージ取得エラー : %d\n", status);
  }

  /* データ操作ハンドルの解放 */
  if ( statementHandle != NULL ) {
    status = ShunFreeHandle( statementHandle );
    if ( status != SHUN_SUCCESS ) {
      printf("データ操作ハンドル解放エラー : %d\n", status);
    }
  }

  /* コネクションの切断 */
  if ( connectionHandle != NULL) {
    status = ShunDisconnect( connectionHandle );
    if ( status != SHUN_SUCCESS ) {
      printf("コネクション切断エラー : %d\n", status);
     }
  }

  /* コネクションハンドルの解放 */
  if ( connectionHandle != NULL) {
    status = ShunFreeHandle( connectionHandle );
    if ( status != SHUN_SUCCESS ) {
      printf("コネクションハンドル解放エラー : %d\n", status);
    }
  }

  return 1;
}

実行結果

追加成功