1. #include "stdio.h"    

  2. int handle_error(SednaConnection* conn,    
  3.                  const char* op,    
  4.                  int close_connection) {    
  5.     printf("%s failed: \\n%s\\n", op, SEgetLastErrorMsg(conn));    
  6.     if(close_connection == 1) SEclose(conn);    
  7.     return -1;    
  8. }    

  9. int main() {    
  10.   struct SednaConnection conn = SEDNA_CONNECTION_INITIALIZER;    
  11.   int bytes_read, res, value;    
  12.   char buf[1024];    

  13.   /* Turn off autocommit mode */    
  14.   value = SEDNA_AUTOCOMMIT_OFF;    
  15.   res = SEsetConnectionAttr(&conn, SEDNA_ATTR_AUTOCOMMIT,    
  16.                             (void*)&value, sizeof(int));    

  17.   /* Connect to the database */    
  18.   res = SEconnect(&conn, "localhost", "test_db",    
  19.                   "SYSTEM", "MANAGER");    
  20.   if(res != SEDNA_SESSION_OPEN)    
  21.     return handle_error(&conn, "Connection", 0);    

  22.   /* Begin a new transaction */    
  23.   res = SEbegin(&conn);    
  24.   if(res != SEDNA_BEGIN_TRANSACTION_SUCCEEDED)    
  25.     return handle_error(&conn, "Transaction begin", 1);    

  26.   /* Load file "region.xml" into the document "region" */    
  27.   res = SEexecute(&conn, "LOAD 'region.xml' 'region'");    
  28.   if(res != SEDNA_BULK_LOAD_SUCCEEDED)    
  29.     return handle_error(&conn, "Bulk load", 1);    

  30.   /* Execute XQuery statement */    
  31.   res = SEexecute(&conn, "doc('region')/*/*");    
  32.   if(res != SEDNA_QUERY_SUCCEEDED)    
  33.     return handle_error(&conn, "Query", 1);    

  34.   /* Iterate and print the result sequence */    
  35.   while((res = SEnext(&conn)) != SEDNA_RESULT_END) {    
  36.     if (res == SEDNA_ERROR)    
  37.       return handle_error(&conn, "Getting item", 1);    

  38.     do {    
  39.       bytes_read = SEgetData(&conn, buf, sizeof(buf) - 1);    
  40.       if(bytes_read == SEDNA_ERROR)    
  41.         return handle_error(&conn, "Getting item", 1);    
  42.       buf[bytes_read] = '\\0';    
  43.       printf("%s\\n", buf);    
  44.     } while(bytes_read > 0);    
  45.   }    

  46.   /* Drop document "region" */    
  47.   res = SEexecute(&conn, "DROP DOCUMENT 'region'");    
  48.   if(res != SEDNA_UPDATE_SUCCEEDED)    
  49.     return handle_error(&conn, "Drop document", 1);    

  50.   /* Commit transaction */    
  51.   res = SEcommit(&conn);    
  52.   if(res != SEDNA_COMMIT_TRANSACTION_SUCCEEDED)    
  53.     return handle_error(&conn, "Commit", 1);    

  54.   /* Close connection */    
  55.   res = SEclose(&conn);    
  56.   if(res != SEDNA_SESSION_CLOSED)    
  57.     return handle_error(&conn, "Close", 0);    

  58.   return 0;    
  59. }