Linux-MySql

  • MySql
    • 1:连接
      • 1 #include <mysql.h>
        2 MYSQL *mysql_init(MYSQL *);
        3 MYSQL *mysql_real_connect(MYSQL *connection,const char *server_host,......);
        4 void mysql_close(MYSQL *connection);
        5 int mysql_options(MYSQL* connection, enum option_to_set const char* argument);
    • 2:错误处理
      • 1 //错误处理函数
      • 2 unsigned int mysql_errno(MYSQL *connection); 
      • 3 char *mysql_error(MYSQL *connection); 
    • 3:执行SQL语句
      • 1 //success 0
        2 int mysql_query(MYSQL *connection, const char *query); 
        3 my_ulonglong mysql_affected_rows(MYSQL *connection);
    • 4.返回数据的语句
      • //一次提取所有的数据
        MYSQL_RES *mysql_store_result(MYSQL *connection);
        //返回记录的条目
        my_ulonglong mysql_num_rows(MYSQL_RES* result);
        //从mysql_store_result中得到的结构中提取一行
        MYSQL_ROW mysql_fetch_row(MYSQL_RES *result);
        //在结果集中跳转,设置将被下一个mysql_fetch_row操作返回的行
        void mysql_data_seek(MYSQL_RES *result,my_ulonglong offset);
        //返回一个偏移值,不是行号,不能用于mysql_data_seek
        MYSQL_ROW_OFFSET mysql_row_tell(MYSQL_RES *result);
        //在结果集中移动当前位置,并返回之前的位置
        MYSQL_ROW_OFFSET mysql_row_seek(MYSQL_RES *result,MYSQL_ROW_OFFSET offset);
        //清理结果集
        void mysql_free_result(MYSQL_RES *result);
    • 5.处理返回的数据
      • 1 //MySQL返回两种类型的数据,列数据和元数据
        2 
        3 //查询结果的基本信息,返回结果集中的字段(列)数目
        4 unsigned int mysql_field_count(MYSQL *connection);
    • Example
      •  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_row()
        11 {
        12     unsigned int field_count;
        13     field_count = 0;
        14     while(field_count < mysql_field_count(&my_connection))
        15     {
        16         printf("%s ",sqlrow[field_count]);
        17         field_count++;
        18     }
        19     printf("
        ");
        20 }
        21 int main(int argc, char *argv[])
        22 {
        23     int res;
        24     
        25     mysql_init(&my_connection);
        26     if(mysql_real_connect(&my_connection,"localhost","rick","secret","foo",0,NULL,0))
        27     {
        28         printf("Connection success
        ");
        29         res = mysql_query(&my_connection,"select childno, fname,age from children where age > 5");
        30         if(res)
        31         {
        32             printf("select error:%s
        ",mysql_error(&my_connection));
        33         }
        34         else
        35         {
        36             res_ptr = mysql_store_result(&my_connection);
        37             if(res_ptr)
        38             {
        39                 printf("Retrieved %lu rows
        ",(unsigned long)mysql_num_rows(res_ptr));
        40                 while((sqlrow = mysql_fetch_row(res_ptr)))
        41                 {
        42                     printf("Fetched data...
        ");
        43                     display_row();
        44                 }
        45                 if(mysql_errno(&my_connection))
        46                 {
        47                     fprintf(stderr,"Retrive error:%s
        ",mysql_error(&my_connection));
        48                 }
        49                 mysql_free_result(res_ptr);
        50             }
        51         }
        52         mysql_close(&my_connection);
        53     }
        54     else
        55     {
        56         fprintf(stderr,"Connection failed
        ");
        57         if(mysql_errno(&my_connection))
        58         {
        59             fprintf(stderr,"Connection error %d:%s
        ",mysql_error(&my_connection));
        60         }
        61     }
        62     return EXIT_SUCCESS;
        63 }
        • 编译:
          •  首先查看mysql.h的安装位置
            • locate  mysql.h
            • g++ main.cpp  MysqlHelper.cpp -I /usr/local/mysql/include/ -L /usr/local/mysql/lib/ -lmysqlclient
原文地址:https://www.cnblogs.com/zhaohu/p/9042254.html