人大金仓KCI

#include "bin/libkci.h"

static void exit_nicely(KCIConnection *conn)
{
   KCIConnectionDestory(conn);
   exit(1);
}

int main()
{
   KCIConnection *conn;
   KCIResult *res;
   int nFields;
   int i, j;

   conninfo = "host = localhost port = 54321 dbname = TEST user = SYSTEM password = SYSTEM";
   conn = KCIConnectionCreate(conninfo);
//   conn = KCIConnectionCreateDeprecated("localhost", "54321", NULL, NULL , "TEST", "SYSTEM", "SYSTEM", "0");

   /* Check to see that the backend connection was successfully made */
   if(KCIConnectionGetStatus(conn) != CONNECTION_OK)
   {
      fprintf(stderr, "Connection to database failed: %s",
              KCIConnectionGetLastError(conn));
      exit_nicely(conn);
   }

   /*
    * Our test case here involves using a cursor, for which we must be inside
    * a transaction block.  We could do the whole thing with a single
    * KCIStatementExecute() of "select * from sys_database", but that's too trivial to make
    * a good example.
    */
   /* Start a transaction block */
   res = KCIStatementExecute(conn, "BEGIN");

   if(KCIResultGetStatusCode(res) != EXECUTE_COMMAND_OK)
   {
      fprintf(stderr, "BEGIN command failed: %s", KCIConnectionGetLastError(conn));
      KCIResultDealloc(res);
      exit_nicely(conn);
   }

   /*
    * Should clear KCIResult whenever it is no longer needed to avoid memory
    * leaks
    */
   KCIResultDealloc(res);
   /*
    * Fetch rows from sys_database, the system catalog of databases
    */
   res = KCIStatementExecute(conn, "DECLARE myportal CURSOR FOR select * from sys_database");

   if(KCIResultGetStatusCode(res) != EXECUTE_COMMAND_OK)
   {
      fprintf(stderr, "DECLARE CURSOR failed: %s", KCIConnectionGetLastError(conn));
      KCIResultDealloc(res);
      exit_nicely(conn);
   }

   KCIResultDealloc(res);
   res = KCIStatementExecute(conn, "FETCH ALL in myportal");

   if(KCIResultGetStatusCode(res) != EXECUTE_TUPLES_OK)
   {
      fprintf(stderr, "FETCH ALL failed: %s", KCIConnectionGetLastError(conn));
      KCIResultDealloc(res);
      exit_nicely(conn);
   }

   /* first, print out the attribute names */
   nFields = KCIResultGetColumnCount(res);

   for(i = 0; i < nFields; i++)
   {
      printf("%-15s", KCIResultGetColumnName(res, i));
   }

   printf("\n\n");

   /* next, print out the rows */
   for(i = 0; i < KCIResultGetRowCount(res); i++)
   {
      for(j = 0; j < nFields; j++)
      {
         printf("%-15s", KCIResultGetColumnValue(res, i, j));
      }

      printf("\n");
   }

   KCIResultDealloc(res);
   /* close the portal ... we don't bother to check for errors ... */
   res = KCIStatementExecute(conn, "CLOSE myportal");
   KCIResultDealloc(res);
   /* end the transaction */
   res = KCIStatementExecute(conn, "END");
   KCIResultDealloc(res);
   /* close the connection to the database and cleanup */
   KCIConnectionDestory(conn);
   return 0;
}

编译需要链接库libkci.so

posted @ 2015-09-25 10:49  thomas_blog  阅读(467)  评论(0编辑  收藏  举报