c语言调用Mysql数据库

一、mysql数据库的C语言常用接口API
1)初始化MYSQL句柄

         MYSQL *pConn;
         pConn = mysql_init(NULL);
2)连接数据库

       // 第2、3、4、5参数的意思分别是:服务器地址、用户名、密码、数据库名,第6个为mysql端口号(0为默认值3306)
 

        if(!mysql_real_connect(pConn,"localhost","root","root","test",0,NULL,0))
        {  
                printf("无法连接数据库:%s",mysql_error(pConn));
                return;
       }
3)操作数据库

          //第1个参数为数据库句柄,第2个参数为操作数据库语句

          mysql_query(pConn,"set names gbk");//防止乱码。设置和数据库的编码一致就不会乱码
          //mysql_real_query比mysql_query多了个参数: 字符串query的长度, 所以适合有二进制数据的query, 而mysql_query的字符串query不能包含二进制,因为它以\0为结尾

         if(mysql_query(pConn,"select * from persons"))
        {
              printf("查询失败:%s",mysql_error(pConn));                          //函数返回上一个 MySQL 操作产生的文本错误信息

             return;
        }

        //MYSQL_RES *mysql_store_result(MYSQL *connection): 是把查询结果一次性取到客户端的离线数据集,当结果比较大时耗内存。
       //mysql_use_result则是查询结果放在服务器上,客户端通过指针逐行读取,节省客户端内存。但是一个MYSQL*连接同时只能有一个未关闭的mysql_use_result查询
      // 这个函数接受由mysql_store_result返回的结果结构集,并返回结构集中的行数 my_ulonglong mysql_num_rows(MYSQL_RES *result);

     // 这个函数从使用mysql_store_result得到的结果结构中提取一行,并把它放到一个行结构中。当数据用完或发生错误时返回NULL.

MYSQL_ROW mysql_fetch_row(MYSQL_RES *resutl);

     // 这个函数接受由mysql_store_result返回的结果结构集,并返回结构集中的行数 my_ulonglong mysql_num_rows(MYSQL_RES *result);

      // 返回结果集中的字段(列)数目 unsigned int mysql_field_count(MYSQL *connection); 

     // 这个函数用来覆盖当前的字段编号,该编号会随着每次mysql_fetch_field调用而自动增加。如果给offset传递0,那么将跳回第1列
MYSQL_FIELD_OFFSET mysql_field_seek(MYSQL *result, MYSQL_FIELD_OFFSET offset);

    //返回数据库中变更的行数:mysql_affected_rows(MYSQL *);
   //返回一个偏移值,它用来表示结果集中的当前位置。它不是行号,MYSQL_ROW_OFFSET mysql_row_tell(MYSQL_RES *result); 
   // 这将在结果集中移动当前的位置,并返回之前的位置 MYSQL_ROW_OFFSET mysql_row_seek(MYSQL_RES *result, MYSQL_ROW_OFFSET offset);
  

4)别忘了free和close

 mysql_free_result(result); //释放查询结果集,
 mysql_close(pConn);       //关闭连接

 

使用代码

 #include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include "mysql.h"

#include "errmsg.h"

#include "mysqld_error.h"

 

MYSQL conn;

MYSQL_RES *res_ptr;

MYSQL_ROW sqlrow;

 

void connection(const char* host, const char* user, const char* password, const char* database)

{

             mysql_init(&conn); // 注意取地址符&

             if (mysql_real_connect(&conn, host, user, password, database, 0, NULL, 0))

             {

                       printf("Connection success!\n");

            }

          else

          {

                              fprintf(stderr, "Connection failed!\n");

                              if (mysql_errno(&conn))

                             {

                                         fprintf(stderr, "Connection error %d: %s\n", mysql_errno(&conn), mysql_error(&conn));       //错误编号,错误信息

                             }

                            exit(EXIT_FAILURE);

         }

}

void display_row()

{

            unsigned int field_count = mysql_field_count(&conn);

            int i = 0;

            while (i < field_count)

            {

                        if (sqlrow[i])

                                printf("%s ", sqlrow[i]);

                      else

                               printf("NULL"); i++;

          }

          printf("\n");

}

 

 int main (int argc, char *argv[])

{

           connection("localhost", "root", "shuang", "shuangde");

           int res = mysql_query(&conn, "SELECT * from student");

           if (res)

          {

                         fprintf(stderr, "SELECT error: %s\n", mysql_error(&conn));

         }

         else

        {

                   res_ptr = mysql_use_result(&conn);

                   if (res_ptr)

                   {

                                     int first = 1;

                                     while ((sqlrow = mysql_fetch_row(res_ptr)))

                                     {

                                                  display_row();

                                     }

                                     if (mysql_errno(&conn))

                                     {

                                                fprintf(stderr, "Retrive error: %s\n", mysql_error(&conn));

                                     }

                                    mysql_free_result(res_ptr);

                    }

      }

     mysql_close(&conn);

     exit(EXIT_SUCCESS);

}

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值