1. #include "libsedna.h"    
  2. #include "stdio.h"    
  3.    
  4. int handle_error(SednaConnection* conn,    
  5.                  const char* op,    
  6.                  int close_connection) {    
  7.     printf("%s failed: \\n%s\\n", op, SEgetLastErrorMsg(conn));    
  8.     if(close_connection == 1) SEclose(conn);    
  9.     return -1;    
  10. }    
  11.    
  12. int main() {    
  13.   struct SednaConnection conn = SEDNA_CONNECTION_INITIALIZER;    
  14.   int bytes_read, res, value;    
  15.   char buf[1024];    
  16.    
  17.   /* Turn off autocommit mode */    
  18.   value = SEDNA_AUTOCOMMIT_OFF;    
  19.   res = SEsetConnectionAttr(&conn, SEDNA_ATTR_AUTOCOMMIT,    
  20.                             (void*)&value, sizeof(int));    
  21.    
  22.   /* Connect to the database */    
  23.   res = SEconnect(&conn, "localhost""test_db",    
  24.                   "SYSTEM""MANAGER");    
  25.   if(res != SEDNA_SESSION_OPEN)    
  26.     return handle_error(&conn, "Connection", 0);    
  27.    
  28.   /* Begin a new transaction */    
  29.   res = SEbegin(&conn);    
  30.   if(res != SEDNA_BEGIN_TRANSACTION_SUCCEEDED)    
  31.     return handle_error(&conn, "Transaction begin", 1);    
  32.    
  33.   /* Load file "region.xml" into the document "region" */    
  34.   res = SEexecute(&conn, "LOAD 'region.xml' 'region'");    
  35.   if(res != SEDNA_BULK_LOAD_SUCCEEDED)    
  36.     return handle_error(&conn, "Bulk load", 1);    
  37.    
  38.   /* Execute XQuery statement */    
  39.   res = SEexecute(&conn, "doc('region')/*/*");    
  40.   if(res != SEDNA_QUERY_SUCCEEDED)    
  41.     return handle_error(&conn, "Query", 1);    
  42.    
  43.   /* Iterate and print the result sequence */    
  44.   while((res = SEnext(&conn)) != SEDNA_RESULT_END) {    
  45.     if (res == SEDNA_ERROR)    
  46.       return handle_error(&conn, "Getting item", 1);    
  47.    
  48.     do {    
  49.       bytes_read = SEgetData(&conn, buf, sizeof(buf) - 1);    
  50.       if(bytes_read == SEDNA_ERROR)    
  51.         return handle_error(&conn, "Getting item", 1);    
  52.       buf[bytes_read] = '\\0';    
  53.       printf("%s\\n", buf);    
  54.     } while(bytes_read > 0);    
  55.   }    
  56.    
  57.   /* Drop document "region" */    
  58.   res = SEexecute(&conn, "DROP DOCUMENT 'region'");    
  59.   if(res != SEDNA_UPDATE_SUCCEEDED)    
  60.     return handle_error(&conn, "Drop document", 1);    
  61.    
  62.   /* Commit transaction */    
  63.   res = SEcommit(&conn);    
  64.   if(res != SEDNA_COMMIT_TRANSACTION_SUCCEEDED)    
  65.     return handle_error(&conn, "Commit", 1);    
  66.    
  67.   /* Close connection */    
  68.   res = SEclose(&conn);    
  69.   if(res != SEDNA_SESSION_CLOSED)    
  70.     return handle_error(&conn, "Close", 0);    
  71.    
  72.   return 0;    
  73. }   

本文转载自 http://www.itjianghu.net/120109/4091743422453720.htm