socks

以下函数使用之前需安装mysql,并包含mysql.h头文件,设置好mysqlclient动态库

一、mysql_init()

MYSQL * mysql_init(MYSQL *mysql);    // 初始化一个MYSQL 连接的实例对象
这个函数有两种用法:
1、参数传NULL 值。
    // 这种情况很显然,是mysql_init() 函数内部申请了一片内存,然后返回了首地址。
    MYSQL *ms_conn = mysql_init(NULL);
    // 用完记得要释放
    mysql_close(ms_conn), ms_conn = NULL;

2、参数传对象地址。
    // 这种情况就是使用栈内存,mysql_init() 函数显然不应该给分配堆内存。
    MYSQL ms_eg; 
    MYSQL *ms_conn = mysql_init(&ms_eg);
    // 用完记得要释放
    mysql_close(ms_conn), ms_conn = NULL;

 

二、mysql_real_connect()

#include<mysql.h>
函数原型描述:
MYSQL *mysql_real_connect (MYSQL *mysql,  //mysql_init()函数初始化获得的mysql实例对象 
const char *host,                //主机名或IP地址
const char *user,                //登录mysql的用户名
const char *passwd,               //登录mysql的密码
const char *db,                 //设置连接指定的的数据库
unsigned int port,               //端口,如果“port”不是0,其值将用作TCP/IP连接的端口号
const char *unix_socket,           //如果unix_socket不是NULL,该字符串描述了应使用的套接字或命名管道
unsigned long client_flag)          //client_flag的值通常为0,但是,也能将其设置为下述标志的组合,以允许特定功能

//一些个标志

  标志描述 CLIENT_COMPRESS

  使用压缩协议。 CLIENT_FOUND_ROWS

  返回发现的行数(匹配的),而不是受影响的行数。 CLIENT_IGNORE_SPACE

  允许在函数名后使用空格。使所有的函数名成为保留字。 CLIENT_INTERACTIVE

  关闭连接之前,允许interactive_timeout秒的不活动时间。 CLIENT_LOCAL_FILES

  允许LOAD DATA LOCAL处理功能。 CLIENT_MULTI_STATEMENTS

  通知服务器,客户端可能在单个字符串内发送多条语句。 CLIENT_MULTI_RESULTS

  通知服务器,客户端能够处理来自多语句执行。 CLIENT_NO_SCHEMA

  禁止db_name.tbl_name.col_name语法。 CLIENT_ODBC

  客户端是ODBC客户端。它将mysqld变得更为ODBC友好。 CLIENT_SSL



如果连接成功,返回MYSQL*连接句柄。如果连接失败,返回NULL。对于成功的连接,返回值与第1个参数的值相同。

用法:
MYSQL *ms_conn = mysql_init(NULL);
if(mysql_real_connect(ms_conn,"localhost","user_name","user_password"
            ,"testdb",0,NULL,0)==nullprt){
  std::cout<<"mysql real_connect failed!"<<std::endl;
  mysql_close(ms_conn);
}

 

三、mysql_query()

函数原型描述:
    int mysql_query(MYSQL *mysql,const char *q)  //执行*q的sql语句,结果返回至mysql,失败返回非0
用法:
  std::string sql("select name,password from user;");
  if(mysql_query(ms_conn,sql.c_str())){
    //执行失败
    std::cout<<"query failed "<<mysql_error(&ms_conn)<<std::endl;
  }

 

四、mysql_store_result()

函数原型描述
    MYSQL_RES *mysql_store_result(MYSQL *mysql)  //将查询的全部结果读取到客户端,分配1个MYSQL_RES结构,并将结果置于该结构中 如果查询失败,返回null指针
用法:
  MYSQL_RES* result=mysql_store_result(ms_conn);
  if(result==nullptr){
    std::cout<<"mysql_store_result failed "<<mysql_error(ms_conn)<<std::endl;
  }

 

五、mysql_use_result()

函数原型描述:
    MYSQL_RES * STDCALL mysql_use_result(MYSQL *mysql); //逐条进行查询,逐条将结果返回给客户端 当每条记录的数据特别大,不适用mysql_store_result()时使用
用法:
    MYSQL_RES *result = mysql_use_result(mysql);
  if(result){
     while(row = mysql_fetch_row(result))
        {
            //row = mysql_fetch_row(result);
            if(row==NULL)
                break;
            for(t=0;t<mysql_num_fields(result);++t)
                printf("%s ",row[t]);
            printf("\n");
        }
  }

 

六、mysql_fetch_row()


函数原型描述:
    MYSQL_ROW STDCALL mysql_fetch_row(MYSQL_RES *result); //从结果集中取得下一行数据
用法:
   //typedef char **MYSQL_ROW;        /* return data as array of strings */ mysql.h定义的MYSQL_ROW原型
   MYSQL_ROW row = mysql_fetch_row(result);    
   if(row){
     std::cout<<"row.name="<<row[0]<<std::endl;
     std::cout<<"row.password="<<row[1]<<std::endl;
   }
更常用一点的用法
  while(row = mysql_fetch_row(result))
  {
    std::cout<<"row.name="<<row[0]<<std::endl;
    std::cout<<"row.password="<<row[1]<<std::endl;
  }


 

七、mysql_num_fields() 和mysql_num_rows()

mysql_num_fields()函数原型描述
    unsigned int STDCALL mysql_num_fields(MYSQL_RES *result);  //获取结果集中的列的数量
mysql_num_rows()函数原型描述
    unsigned int STDCALL mysql_num_rows(MYSQL_RES *result);  //获取结果集中的行的数量

 

八、mysql_fetch_row()

函数原型:
    unsigned int STDCALL mysql_field_count(MYSQL *mysql);  //返回最近查询的结果列的数量。
用法:
  int sql_num=mysql_field_count(ms_conn);

 

九、mysql_list_fields()

函数原型描述:
    MYSQL_RES * STDCALL mysql_list_fields(MYSQL *mysql, const char *table, const char *wild);  //返回匹配一个简单的正则表达式的列名。
用法:
  MYSQL_RES *tbl_cols = mysql_list_fields(ms_conn, "mytbl", "f%");
  unsigned int field_cnt = mysql_num_fields(tbl_cols);
  for (int i=0; i < field_cnt; ++i) { /* col describes i-th column of the table */
    MYSQL_FIELD *col = mysql_fetch_field_direct(tbl_cols, i);
    printf ("Column %d: %s\n", i, col->name);
  }

 

十、mysql_fetch_field_direct()

函数原型描述
    MYSQL_FIELD *STDCALL mysql_fetch_field_direct(MYSQL_RES *res,unsigned int fieldnr);  //返回一个表字段的类型,给出一个字段编号。 
用法:
  unsigned int num_fields; unsigned int i;
  MYSQL_FIELD *field; num_fields = mysql_num_fields(result);
  for(i = 0; i < num_fields; i++) {
    field = mysql_fetch_field_direct(result, i);
    printf("Field %u is %s\n", i, field->name);
  }

 

十一、mysql_eof()

函数原型描述
    my_bool STDCALL mysql_eof(MYSQL_RES *res);  //确定是否已经读到一个结果集合的最后一行
用法
  mysql_query(&mysql,"SELECT * FROM some_table");
  result = mysql_use_result(&mysql);
  while((row = mysql_fetch_row(result))) {
    // do something with data
  }
  if(!mysql_eof(result)) // mysql_fetch_row() failed due to an error
  {
    fprintf(stderr, "Error: %s\n", mysql_error(&mysql));
  }

 

十二、mysql_error()

函数原型描述  
    const char * STDCALL mysql_error(MYSQL *mysql);  //返回最近被调用的MySQL函数的出错消息
用法
  if(*mysql_error(&mysql)) {
  // an error occurred
  }
  if(mysql_error(&mysql)[0]) {
  // an error occurred
  }

 

十三、mysql_free_result()

函数原型描述
    void STDCALL mysql_free_result(MYSQL_RES *result);  //释放一个结果集合使用的内存。
用法:
  mysql_free_result(ms_conn)

 

十四、MYSQL_RES 结构体

数据原型:
    typedef struct st_mysql_res {
        my_ulonglong  row_count;
        MYSQL_FIELD    *fields;
        MYSQL_DATA    *data;
        MYSQL_ROWS    *data_cursor;
        unsigned long *lengths;        /* column lengths of current row */
        MYSQL        *handle;        /* for unbuffered reads */
        const struct st_mysql_methods *methods;
        MYSQL_ROW    row;            /* If unbuffered read */
        MYSQL_ROW    current_row;        /* buffer to current row */
        MEM_ROOT    field_alloc;
        unsigned int    field_count, current_field;
        my_bool    eof;            /* Used by mysql_fetch_row */
        /* mysql_stmt_close() had to cancel this result */
        my_bool       unbuffered_fetch_cancelled;  
        void *extension;
    } MYSQL_RES;    

 

 

本文链接:https://www.cnblogs.com/socks/p/13084027.html

相关文章: