• C++访问mysql数据库


    C++连接mysql数据库,并取数据进行显示
    本例中,在mysql中已经存在了一个数据库test,并在test数据库中创建了一张表stu做测试,表中包含3个字段

    需要把mysql目录下的libmysql.lib拷贝到当前目录

    代码如下:

      1 /*
      2 ** C语言连接mysql
      3 */
      4 #include <winsock.h>
      5 #include <mysql.h>
      6 #include <iostream>
      7 #include <list>
      8 using namespace std;
      9 
     10 #pragma comment(lib, "libmysql.lib")
     11 // 定义全局变量
     12 MYSQL mysql;
     13 
     14 struct RecvStruct
     15 {
     16     char stuID[50];
     17     char stuName[50];
     18     char stuAge[50];
     19 };
     20 
     21 // 建立连接
     22 BOOL ConnDB(const char *host, const char *user, const char *passwd, const char *dbName)        
     23 {
     24     // 初始化MYSQL结构
     25     mysql_init(&mysql);
     26     // 连接数据库
     27     if(mysql_real_connect(&mysql, host, user, passwd, dbName, 0, NULL, 0) != NULL)
     28     {
     29         // 成功建立连接
     30         return TRUE;
     31     }
     32     else
     33     {
     34         // 建立连接失败
     35         return FALSE;
     36     }
     37 }
     38 // 取出数据
     39 list<RecvStruct> GetAllRecv()
     40 {
     41     // 用于保存结果
     42     std::list<RecvStruct> recList;
     43     // 查询语句
     44     std::string strsql = "select * from stu";
     45     // 执行查询
     46     if(mysql_real_query(&mysql, strsql.data(), strsql.length()) == 0)
     47     {
     48         // 取得查询结果
     49         MYSQL_RES *pRes = NULL;
     50         pRes = mysql_store_result(&mysql);
     51         if(pRes != NULL)
     52         {
     53             // 取得结果
     54             MYSQL_ROW row;
     55             while(row = mysql_fetch_row(pRes))
     56             {
     57                 // 保存信息
     58                 RecvStruct rs;
     59                 if(row[0] != NULL)
     60                 {
     61                     strcpy(rs.stuName, row[0]);
     62                 }
     63                 if(row[1] != NULL)
     64                 {
     65                     strcpy(rs.stuID, row[1]);
     66                 }
     67                 if(row[2] != NULL)
     68                 {
     69                     strcpy(rs.stuAge, row[2]);
     70                 }
     71                 // 加入list
     72                 recList.push_back(rs);
     73             }
     74         }
     75     }
     76     return recList;
     77 }
     78 // 关闭数据库
     79 void MysqlClose()
     80 {
     81     // 关闭数据库,释放资源
     82 }
     83 
     84 int main()
     85 {
     86     // 连接数据库
     87     if(ConnDB("127.0.0.1", "root", "123456", "test"))
     88     {
     89         // 成功
     90         std::list<RecvStruct> recvList = GetAllRecv();
     91         // 显示记录
     92         for(list<RecvStruct>::iterator it = recvList.begin();it != recvList.end();it++)
     93         {
     94             cout << it->stuName << "	" << it->stuID << "	" << it->stuAge << endl;
     95         }
     96 
     97         MysqlClose();
     98     }
     99     int i;
    100     cin >> i;
    101     return 0;
    102 }
    View Code
  • 相关阅读:
    迭代器,可迭代对象,生成器区别
    七大经典排序算法
    二叉排序树的插入、生成、删除及查找操作
    二分查找(折半查找)
    顺序查找
    二叉树的创建、遍历及应用
    (原创)一些常见小程序(C)
    顺序队列
    二叉树的创建
    Vue开源项目库汇总
  • 原文地址:https://www.cnblogs.com/lit10050528/p/3459877.html
Copyright © 2020-2023  润新知