C语言连接并操作Sedna XML数据库的方法
本文实例讲述了C语言连接并操作SednaXML数据库的方法。分享给大家供大家参考。具体如下:
#include"libsedna.h" #include"stdio.h" inthandle_error(SednaConnection*conn, constchar*op, intclose_connection){ printf("%sfailed:\n%s\n",op,SEgetLastErrorMsg(conn)); if(close_connection==1)SEclose(conn); return-1; } intmain(){ structSednaConnectionconn=SEDNA_CONNECTION_INITIALIZER; intbytes_read,res,value; charbuf[1024]; /*Turnoffautocommitmode*/ value=SEDNA_AUTOCOMMIT_OFF; res=SEsetConnectionAttr(&conn,SEDNA_ATTR_AUTOCOMMIT, (void*)&value,sizeof(int)); /*Connecttothedatabase*/ res=SEconnect(&conn,"localhost","test_db", "SYSTEM","MANAGER"); if(res!=SEDNA_SESSION_OPEN) returnhandle_error(&conn,"Connection",0); /*Beginanewtransaction*/ res=SEbegin(&conn); if(res!=SEDNA_BEGIN_TRANSACTION_SUCCEEDED) returnhandle_error(&conn,"Transactionbegin",1); /*Loadfile"region.xml"intothedocument"region"*/ res=SEexecute(&conn,"LOAD'region.xml''region'"); if(res!=SEDNA_BULK_LOAD_SUCCEEDED) returnhandle_error(&conn,"Bulkload",1); /*ExecuteXQuerystatement*/ res=SEexecute(&conn,"doc('region')/*/*"); if(res!=SEDNA_QUERY_SUCCEEDED) returnhandle_error(&conn,"Query",1); /*Iterateandprinttheresultsequence*/ while((res=SEnext(&conn))!=SEDNA_RESULT_END){ if(res==SEDNA_ERROR) returnhandle_error(&conn,"Gettingitem",1); do{ bytes_read=SEgetData(&conn,buf,sizeof(buf)-1); if(bytes_read==SEDNA_ERROR) returnhandle_error(&conn,"Gettingitem",1); buf[bytes_read]='\0'; printf("%s\n",buf); }while(bytes_read>0); } /*Dropdocument"region"*/ res=SEexecute(&conn,"DROPDOCUMENT'region'"); if(res!=SEDNA_UPDATE_SUCCEEDED) returnhandle_error(&conn,"Dropdocument",1); /*Committransaction*/ res=SEcommit(&conn); if(res!=SEDNA_COMMIT_TRANSACTION_SUCCEEDED) returnhandle_error(&conn,"Commit",1); /*Closeconnection*/ res=SEclose(&conn); if(res!=SEDNA_SESSION_CLOSED) returnhandle_error(&conn,"Close",0); return0; }
希望本文所述对大家的C语言程序设计有所帮助。