Mysql C 带事务管理的添删查改

作者在 2011-10-06 21:03:34 发布以下内容
今天写的例子,只贴代码,不使用文字说明.
mysqltool.h
#include <stdio.h>
#include <stdlib.h>
#include <winsock.h>
#include <mysql.h>


int xinsert(MYSQL *mysql,char *strsql)
{    
     int t;
     MYSQL_RES *res;
     t=mysql_real_query(mysql,strsql,(unsigned int)strlen(strsql));

     if(t){
           printf( "Error id=%d  Error: %s\n",mysql_errno(mysql),mysql_error(mysql));
           return mysql_errno(mysql);
     }else{
         res=mysql_store_result(mysql);  
         printf("插入行数=%d\n",mysql_affected_rows(mysql));
         mysql_free_result(res);
     }

     return 0;

}

int xupate(MYSQL *mysql,char *strsql)
{    
     int t;
     MYSQL_RES *res;
     t=mysql_real_query(mysql,strsql,(unsigned int)strlen(strsql));
     if(t){
           printf( "Error id=%d  Error: %s\n",mysql_errno(mysql),mysql_error(mysql));
           return mysql_errno(mysql);
     }else{
         res=mysql_store_result(mysql);  
         printf("更新行数=%d\n",mysql_affected_rows(mysql));
         mysql_free_result(res);
     }
     return 0;
}

int xdelete(MYSQL *mysql,char *strsql)
{    
     int t;
     MYSQL_RES *res;
     t=mysql_real_query(mysql,strsql,(unsigned int)strlen(strsql));
     if(t){
           printf( "Error id=%d  Error: %s\n",mysql_errno(mysql),mysql_error(mysql));
           return mysql_errno(mysql);
     }else{
         res=mysql_store_result(mysql);  
         printf("删行数=%d\n",mysql_affected_rows(mysql));
         mysql_free_result(res);
     }
     return 0;

}
one.c 代码如下:
#include <stdlib.h>
#include <winsock.h>
#include <mysql.h>
#include <stdio.h>
#include <mysqltool.h>

int main()
{
      MYSQL mysql;     //mysql连接
      MYSQL_RES *res; //这个结构代表返回行的一个查询结果集
      MYSQL_ROW row; //一个行数据的类型安全(type-safe)的表示
      char *query;  //查询语句
      int t,r;
      mysql_init(&mysql);

      
      if (!mysql_real_connect(&mysql,"localhost", "lottobar", "123456", "lottobar",3306,NULL,0))
      {
         printf( "Error connecting to database: %s\n",mysql_error(&mysql));
      }  else
         printf("Connected\n");
    
      
      query="SET CHARACTER SET GBK"; //设置编码
      t=mysql_real_query(&mysql,query,(unsigned int)strlen(query));
      if(t)
      {
           printf("编码设置失败\n");
      }
      query=" select * from demo ";
      t=mysql_real_query(&mysql,query,(unsigned int)strlen(query));
      if(t)
      {
          printf("执行查询时出现异常: %s",mysql_error(&mysql));
          
      }else
          printf("[%s] 构建成功 \n",query);
      res=mysql_store_result(&mysql);
      while(row=mysql_fetch_row(res))
      {                              
           for(t=0;t<mysql_num_fields(res);t++)
           {
               printf("%s\t",row[t]);
           }
           printf("\n");
      }
      mysql_free_result(res);
      
      t=mysql_real_query(&mysql,"SET AUTOCOMMIT =0",(unsigned int)strlen("SET AUTOCOMMIT =0"));
      if(t){
            printf("启用手工事务失败\n");
      }else{
            printf("启用手工事务成功\n");
      }
      t=mysql_real_query(&mysql,"Begin ;",(unsigned int)strlen("Begin ;"));
      query="insert into demo(name,age)values('老裴','89')";
      int inset_result=xinsert(&mysql,query);
      query="delete  from demo  where name='老裴'";
      int delete_result=xdelete(&mysql,query);
      query="update demo set name='裴屋村' where name='ccd' and inc_id=13";
      int update_result=xupate(&mysql,query);
      
      query="insert into demo(inc_id,name,age)values(16,'老裴','89')";//执行会出现异常的语句
      int fail_result=xinsert(&mysql,query);
      
      if(inset_result==0 && delete_result==0 && update_result==0 && fail_result==0){
           printf("事务提交\n");
           t=mysql_real_query(&mysql,"COMMIT;",(unsigned int)strlen("COMMIT;"));
      }else{
           printf("事务回滚\n");
           t=mysql_real_query(&mysql,"ROLLBACK;",(unsigned int)strlen("ROLLBACK;"));
      }
      
      return 0;  
}
 
 
引用地址:
C语言 | 阅读 1239 次
文章评论,共0条
游客请输入验证码
文章归档
最新评论