db_function.c
上传用户:bilang918
上传日期:2010-03-24
资源大小:558k
文件大小:4k
源码类别:

网络

开发平台:

Unix_Linux

  1. /* DB_libry 封装函数,连结 sybase */
  2. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  3. /************************************************
  4. * Description : Initialize DB-Library
  5. * Function : db_init()
  6. * Input : 
  7. * Ouput : int 由返回值判断初试化状态
  8. ************************************************/
  9. int DB_init(void)
  10. {
  11. /* Initialize DB-Library. */
  12. if (dbinit() == FAIL) return(-1);
  13. //dbsetversion(DBVERSION_100);
  14. return(1);
  15. }
  16. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  17. /**************************************************
  18. * Description: Connect database server
  19. * Function : db_connect()
  20. * Input : char * Dbname 数据库名称
  21. *   char * YsrName 用户名
  22. *   char * UsrPwd 密码
  23. * Ouput : DBPROCESS * dbproc 获得的process
  24. ***************************************************/
  25. DBPROCESS* DB_connect (char *DbName,char *UsrName,char *UsrPwd) 
  26. {
  27. DBPROCESS *dbproc;
  28.       LOGINREC *login; 
  29. login = dblogin();
  30. DBSETLUSER(login, UsrName);
  31. DBSETLPWD(login, UsrPwd);
  32. //BCP_SETL(login, TRUE) ; //????? 
  33. dbproc = dbopen(login, NULL);
  34. dbloginfree(login); //一定要释放dbloginfree
  35.         
  36.         if(dbproc!=NULL) dbuse(dbproc, DbName);
  37. else return(NULL);
  38.  
  39.         return(dbproc);
  40. }
  41. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  42. /******************************************************
  43. * Description: Disconnect Database Server
  44. * Function : DB_disconnect()
  45. * Input : 
  46. * Output : 
  47. *******************************************************/
  48. void  DB_disconnect(DBPROCESS* dbproc)
  49. {
  50. dbclose(dbproc);
  51. dbexit();
  52. }
  53. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  54. /**********************************************************************
  55. * Description : Send some Sql to the server with dbsqlexec
  56. * Function : DB_sqlexec()
  57. * Input : DBPROCESS * dbporc
  58. *   int Sql_type Sql语句的类型 1 为select 0为其他
  59. *   char * SqlStr         Sql语句
  60. *   int units 预计取得的一条记录的单元个数(不超过20)
  61. *   int records 预计取得的记录数
  62. *         void * temp_space 存储空间的地址
  63. *   int record_len 单元的最大长度(不超过2048)
  64. * Output : int 
  65. ***********************************************************************/
  66. int DB_sqlexec(DBPROCESS* dbproc,char * SqlStr,int Sql_type,int units,int records,int record_len,void* temp_space)
  67. {
  68. //DBPROCESS     *dbproc;       /* Our connection with SQL Server. */
  69.         RETCODE        result_code;
  70. DBCHAR   text[20][2048];
  71. int  i;
  72. int  j;
  73. /* 判断 process 是否为空 */
  74. if (dbproc ==(DBPROCESS*)NULL)
  75. {
  76. printf("DBPROCESS NULL SqlStr:%sn",SqlStr);
  77. return(-1);
  78. }
  79. /* 执行Sql语句 */
  80. dbcmd(dbproc,SqlStr);
  81.         dbsqlexec(dbproc);
  82. /* 判断返回值,是否成功 */
  83. result_code = dbresults(dbproc);
  84. i = 0;
  85. j = 0;
  86. if(result_code == SUCCEED)
  87. {
  88. if(Sql_type == 1)
  89. /* select 型SQL语句 */
  90. {
  91. /* BIND 足够多的单元数 */
  92. while( j < units )
  93. {
  94. dbbind(dbproc, j+1, NTBSTRINGBIND, (DBINT)0,text[j]);
  95. j++;
  96. printf("bind %dn",j);
  97. }
  98. /* 从PROCESS上取得数据 */
  99. while (dbnextrow(dbproc) != NO_MORE_ROWS)
  100. {
  101. j = 0;
  102. if ( i < records )//判断所取数据有否超过预留存储空间
  103. {
  104. while(j < units)
  105. {
  106. /* copy至对应存储空间 */
  107. memcpy(temp_space+i*units*record_len+j*record_len,text[j],record_len);
  108. j++;
  109. }
  110. }
  111. i++;
  112. }
  113. }
  114. /*INSERT,UPDATE型SQL语句*/
  115. else 
  116. {
  117. return(0);
  118. }
  119. return(i);
  120. }
  121. else 
  122. {
  123. printf("sql error sql:%sn",SqlStr);
  124. return(-1);
  125. }
  126. }
  127. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  128. /**************************************************************************
  129. *Description : Get record
  130. *Function : Get_record()
  131. *Intput : void * start_data 
  132. *   char * record
  133. *   int num 第几条记录,从0开始记数
  134. *   int r_len 记录的长度(=在DB_sqlexec()中的 record_len)
  135. *Output : int   0为成功,-1为失败
  136. ***************************************************************************/
  137. int Get_record(void *start_data,char * record,int num,int r_len)
  138. {
  139. memcpy(record,start_data+num*r_len,r_len);   
  140. }
  141. /////////////////////////////////////////////////////////////////////////////////////////////////////////////////