zoukankan      html  css  js  c++  java
  • 执行sql语句后的数据处理api

    链接上mysql之后可以使用一些api对数据库进行一些操作

    1 int mysql_query(MYSQL * connection, const char * query)

    这是执行sql语句的函数,成功的话返回0

    1.不返回数据的sql语句

    对于update,delete,insert等操作

    1 my_ulonglong mysql_affected_rows(MYSQL *connection);

    这个函数返回受到影响的行数

    2.发现插入的内容

    1 select LAST_INSERT_ID();

    可以找到最后一次影响的auto_increment的值

    3.返回数据的语句
    c程序中的数据处理的四个步骤是
        1)执行查询
        2)提取数据
        3)处理数据
        4)必要的清理


    这里提取数据的函数是
    mysql_use_result    //一次返回一行数据
    mysql_store_result    //一次返回所有数据


    这里处理数据的函数是
    mysql_fetch_row


    这里释放查询占用的内存资源的函数是

    mysql_free_result>>>void mysql_free_result(MYSQL_RES * result);
    

     
        ***mysql_store_result函数

    1 MYSQL_RES * mysql_store_result(MYSQL * connection);//成功返回指向结果集的指针,失败返回NULL
    2 my_ulonglong mysql_num_rows(MYSQL_RES * result);//可以得到结果集中的行数
    3 MYSQL_ROW mysql_fetch_row(MYSQL_RES * result);//得到结果集中的一行,放到一个行结构中
    4 void mysql_data_seek(MYSQL_RES * reslut, my_ulonglong offset);//设置下次mysql_fetch_row的位置
    5 //0的时候是下次从第一行开始取行,n-1意味着下次从第n行开始取行结构
    6 MYSQL_ROW_OFFSET mysql_row_tell(MYSQL_RES * result);//返回一个偏移值表示结果集中的当前位置
    7 //不能用于mysql_data_seek中,但是可以用到myslq_row_seek中,如下
    8 MYSQL_ROW_OFFSET mysql_row_seek(MYSQL_RES * result, MYSQL_ROW_OFFSET offset);
    9 //移动当前的位置到offste,返回的是之前的位置


        ***mysql_use_result函数

    1 MYSQL_RES * mysql_use_result(MYSQL * connection);//一次返回一行数据,所以不能使用mysql_num_rows了


    4.处理返回的数据
    mysql提供两种类型的数据
        1)从表格中提取的信息,就是列数据
        2)关于数据的数据,也就是元数据(metadata),例如列名和类型

    1 unsigned int mysql_field_count(MYSQL * connection);//接受连接对象,返回结果集中的字段(列)数目
    2 MYSQL_FIELD * mysql_fetch_field(MYSQL_RES * result);//接受结果集,将元数据和数据提取到一个新的结构中
    3 MYSQL_FIELD_OFFST mysql_field_seek(MYSQL_RES * result, MYSQL_FIELD_OFFSET offset);
    4 //这个函数会随着mysql_fetch_field调用而自动增加,要是给参数offste传递值0,将跳回第一列

    下面是一个综合使用上面的api的例子

      1 #include <stdlib.h>
      2 #include <stdio.h>
      3 
      4 #include "mysql.h"
      5 
      6 MYSQL my_connection;//定义链接结构
      7 MYSQL_RES *res_ptr;//定义指向结果集的指针
      8 MYSQL_ROW sqlrow;//定义行结构
      9 
     10 void display_header();//定义类型打印函数
     11 void display_row();//定义行打印函数
     12 
     13 
     14 int main(int argc, char *argv[]) 
     15 {
     16    int res;
     17    int first_row = 1;
     18 
     19    mysql_init(&my_connection);//初始化连接  
     20    if (mysql_real_connect(&my_connection, "localhost", "root", "", "foo", 0, NULL, 0)) 
     21    {//建立和数据库的连接
     22       printf("Connection success
    ");
     23    
     24       res = mysql_query(&my_connection, "SELECT childno, fname, age FROM children WHERE age > 5");
     25     //执行一条查询语句,针对的是当前的连接
     26       if (res) 
     27       {
     28          fprintf(stderr, "SELECT error: %s
    ", mysql_error(&my_connection));
     29       } else 
     30       {//指定sql语句成功的话
     31          res_ptr = mysql_use_result(&my_connection);//一次一行返回到指向结果集结构的指针
     32          if (res_ptr) 
     33          {//成功的话
     34             display_header();//进入子函数打印相应的结构信息
     35             while ((sqlrow = mysql_fetch_row(res_ptr))) //获取结果集中一行,放到行结构中
     36             {
     37                if (first_row) 
     38                {//恒执行
     39                   display_header();//进入子函数打印相应的结构信息
     40                   first_row = 0;//给整型值赋值为0
     41                }
     42                display_row();//进入子函数打印行信息
     43             }
     44             if (mysql_errno(&my_connection)) 
     45             {//失败的话,打印失败的信息
     46              fprintf(stderr, "Retrive error: %s
    ",mysql_error(&my_connection));
     47             }
     48          }
     49          mysql_free_result(res_ptr);//释放查询占用的内存资源
     50       }
     51       mysql_close(&my_connection);//关闭连接
     52    } 
     53    else 
     54    {//连接失败处理
     55       fprintf(stderr, "Connection failed
    ");
     56       if (mysql_errno(&my_connection)) {
     57         fprintf(stderr, "Connection error %d: %s
    ",
     58                                 mysql_errno(&my_connection),
     59                                 mysql_error(&my_connection));
     60         }
     61     }
     62    
     63    return EXIT_SUCCESS;
     64 }
     65 
     66 
     67 void display_header() {
     68    MYSQL_FIELD *field_ptr;
     69 
     70    printf("Column details:
    ");
     71  
     72    while ((field_ptr = mysql_fetch_field(res_ptr)) != NULL) //传入指向结果集指针,提取元数据和数据到一个新结构中
     73    {
     74       printf("	 Name: %s
    ", field_ptr->name);//打印列名
     75       printf("	 Type: ");//打印类型
     76       if (IS_NUM(field_ptr->type)) 
     77       {
     78          printf("Numeric field
    ");//类型是数值型情况
     79       } else 
     80       {//其他情况
     81          switch(field_ptr->type) 
     82          {
     83             case FIELD_TYPE_VAR_STRING:
     84                printf("VARCHAR
    ");
     85             break;
     86             case FIELD_TYPE_LONG: 
     87                printf("LONG
    ");
     88             break;
     89             default:
     90               printf("Type is %d, check in mysql_com.h
    ", field_ptr->type);
     91          } /* switch */
     92       } /* else */
     93 
     94       printf("	 Max width %ld
    ", field_ptr->length); /* Note on versions of MySQL before 4.0 the f
    ormat should be %d, rather than %ld
    */ 95 if (field_ptr->flags & AUTO_INCREMENT_FLAG) 96 printf(" Auto increments "); 97 printf(" "); 98 } /* while */ 99 } 100 101 102 void display_row() { 103 unsigned int field_count; 104 105 field_count = 0; 106 while (field_count < mysql_field_count(&my_connection)) //列数目个打印 107 { 108 if (sqlrow[field_count]) printf("%s ", sqlrow[field_count]);//对行结构打印 109 else printf("NULL"); 110 field_count++; 111 } 112 printf(" "); 113 }

    执行的结果

     1 jason@t61:~/c_program/544977-blp3e/chapter08$ gcc -I/usr/include/mysql select4.c -L/usr/lib/mysql -lmysqlclient -o select4
     2 jason@t61:~/c_program/544977-blp3e/chapter08$ ./select4 
     3 Connection success
     4 Column details:
     5      Name: childno
     6      Type: Numeric field
     7      Max width 11
     8      Auto increments
     9 
    10      Name: fname
    11      Type: VARCHAR
    12      Max width 30
    13 
    14      Name: age
    15      Type: Numeric field
    16      Max width 11
    17 
    18 Column details:
    19 1 Jenny 17 
    20 2 Andrew 13 
    21 6 Alex 11 
    22 jason@t61:~/c_program/544977-blp3e/chapter08$ 

    万事走心 精益求美


  • 相关阅读:
    我的2012
    java抽象工厂模式
    java工厂方法模式
    javascript闭包
    字符串转换成枚举类型
    按位与 按位或 按位异域
    一个查找替换文件的简单工具
    c#写的.net 画流程图的控件
    .net 面试题之 Sql 分页 存储过程
    .net 面试整理2013年3月21
  • 原文地址:https://www.cnblogs.com/kongchung/p/4606786.html
Copyright © 2011-2022 走看看