加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C语言连接并操作Sedna XML数据库的方法

发布时间:2020-12-16 03:31:03 所属栏目:百科 来源:网络整理
导读:本篇章节讲解C语言连接并操作Sedna XML数据库的方法。供大家参考研究。具体如下: #include "libsedna.h" #include "stdio.h" int handle_error(SednaConnection* conn,const char* op,int close_connection) { printf("%s failed: n%sn",op,SEget

本篇章节讲解C语言连接并操作Sedna XML数据库的方法。分享给大家供大家参考。具体如下:

#include "libsedna.h" 
#include "stdio.h" 
int handle_error(SednaConnection* conn,const char* op,int close_connection) { 
  printf("%s failed: n%sn",op,SEgetLastErrorMsg(conn)); 
  if(close_connection == 1) SEclose(conn); 
  return -1; 
} 
int main() { 
 struct SednaConnection conn = SEDNA_CONNECTION_INITIALIZER; 
 int bytes_read,res,value; 
 char buf[1024]; 
 /* Turn off autocommit mode */ 
 value = SEDNA_AUTOCOMMIT_OFF; 
 res = SEsetConnectionAttr(&conn,SEDNA_ATTR_AUTOCOMMIT,(void*)&value,sizeof(int)); 
 /* Connect to the database */ 
 res = SEconnect(&conn,"localhost","test_db","SYSTEM","MANAGER"); 
 if(res != SEDNA_SESSION_OPEN) 
  return handle_error(&conn,"Connection",0); 
 /* Begin a new transaction */ 
 res = SEbegin(&conn); 
 if(res != SEDNA_BEGIN_TRANSACTION_SUCCEEDED) 
  return handle_error(&conn,"Transaction begin",1); 
 /* Load file "region.xml" into the document "region" */ 
 res = SEexecute(&conn,"LOAD 'region.xml' 'region'"); 
 if(res != SEDNA_BULK_LOAD_SUCCEEDED) 
  return handle_error(&conn,"Bulk load",1); 
 /* Execute XQuery statement */ 
 res = SEexecute(&conn,"doc('region')/*/*"); 
 if(res != SEDNA_QUERY_SUCCEEDED) 
  return handle_error(&conn,"Query",1); 
 /* Iterate and print the result sequence */ 
 while((res = SEnext(&conn)) != SEDNA_RESULT_END) { 
  if (res == SEDNA_ERROR) 
   return handle_error(&conn,"Getting item",1); 
  do { 
   bytes_read = SEgetData(&conn,buf,sizeof(buf) - 1); 
   if(bytes_read == SEDNA_ERROR) 
    return handle_error(&conn,1); 
   buf[bytes_read] = ''; 
   printf("%sn",buf); 
  } while(bytes_read > 0); 
 } 
 /* Drop document "region" */ 
 res = SEexecute(&conn,"DROP DOCUMENT 'region'"); 
 if(res != SEDNA_UPDATE_SUCCEEDED) 
  return handle_error(&conn,"Drop document",1); 
 /* Commit transaction */ 
 res = SEcommit(&conn); 
 if(res != SEDNA_COMMIT_TRANSACTION_SUCCEEDED) 
  return handle_error(&conn,"Commit",1); 
 /* Close connection */ 
 res = SEclose(&conn); 
 if(res != SEDNA_SESSION_CLOSED) 
  return handle_error(&conn,"Close",0); 
 return 0; 
}

希望本文所述对大家的C语言程序设计有所帮助。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读