c++ centos linux mysql通讯案例(安装和验证)

2021/5/19 19:31:46

本文主要是介绍c++ centos linux mysql通讯案例(安装和验证),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

近期需要做c++与mysql通讯,

[root@VM-0-7-centos test]# cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)
[root@VM-0-7-centos test]#

1.  安装mysql-client等软件

# c++开发mysql
# https://downloads.mysql.com/archives/community/
# 移除自带的mariadb
rpm -qa|grep "mariadb"|xargs rpm -e --nodeps
# 安装源
rpm -ivh https://repo.mysql.com//mysql57-community-release-el7-11.noarch.rpm
# 查找是否有对应的客户端
yum search mysql-community
# 下载安装
yum install mysql-community-client.x86_64 -y 
yum reinstall mysql-libs -y
# 安装
yum install mysql-devel -y 
# 查看安装的内容
rpm -qa|grep mysql

 

2. c++链接源码(网上有很多,此处附加了输出行数,列数,解决了数组输出遇到NULL停止输出的bug)

 

main.cpp

#include<iostream>
#include"MyDB.h"
using namespace std;


int main()
{
    MyDB db; 
    //连接数据库
    db.initDB("localhost","root","password","databaseName");//此处可以执行任何sql语句,数据输出到屏幕
    db.exeSQL("SELECT * from redisdata;");

    return 0;
}

 

makefile

all:
    g++  main.cpp MyDB.cpp -o main  `mysql_config --cflags --libs` 

 

MyDB.h

#ifndef _MYDB_H
#define _MYDB_H
#include<iostream>
#include<string>
#include <stack>
#include <algorithm>  
#include<mysql/mysql.h>
using namespace std;

class MyDB
{
    public:
    MyDB();
    ~MyDB();
    bool initDB(string host,string user,string pwd,string db_name); //连接mysql
    bool exeSQL(string sql);   //执行sql语句
    private:
    MYSQL *mysql;          //连接mysql句柄指针
    MYSQL_RES *result;    //指向查询结果的指针
    MYSQL_ROW row;       //按行返回的查询信息
};


#endif

 

MyDB.cpp

#include<iostream>
#include<string>
#include "MyDB.h"

using namespace std;

MyDB::MyDB()
{
    mysql=mysql_init(NULL);   //初始化数据库连接变量
    if(mysql==NULL)
    {
        cout<<"Error:"<<mysql_error(mysql);
        exit(1);
    }
}

MyDB::~MyDB()
{
    if(mysql!=NULL)  //关闭数据连接
    {
        mysql_close(mysql);
    }
}


bool MyDB::initDB(string host,string user,string passwd,string db_name)
{
    // 函数mysql_real_connect建立一个数据库连接  
    // 成功返回MYSQL*连接句柄,失败返回NULL  
    mysql = mysql_real_connect(mysql, host.c_str(), user.c_str(), passwd.c_str(), db_name.c_str(), 0, NULL, 0);  
    if(mysql == NULL)  
    {  
        cout << "Error: " << mysql_error(mysql);  
        exit(1);  
    }  
    return true;  
}

bool MyDB::exeSQL(string sql)
{
    //mysql_query()执行成功返回0,执行失败返回非0值。
    if (mysql_query(mysql,sql.c_str()))
    {
        cout<<"Query Error: "<<mysql_error(mysql);
        return false;
    }
    else // 查询成功
    {
        result = mysql_store_result(mysql);  //获取结果集
        if (result)  // 返回了结果集
        {
           int  num_fields = mysql_num_fields(result);   //获取结果集中总共的字段数,即列数
           int  num_rows=mysql_num_rows(result);       //获取结果集中总共的行数
           cout<<"num_fields:"<<num_fields<<endl;
           cout<<"num_rows:"<<num_rows<<endl;
           for(int i=0;i<num_rows;i++) //输出每一行
            {
                //获取下一行数据
                row=mysql_fetch_row(result);
                if(row<0) break;

                for(int j=0;j<num_fields;j++)  //输出每一字段
                {
                    if(row[j]==NULL)cout<<"null"<<"\t\t";
                    else cout<<row[j]<<"\t\t";
                }
                cout<<endl;
            }

        }
        else  // result==NULL
        {
            if(mysql_field_count(mysql) == 0)   //代表执行的是update,insert,delete类的非查询语句
            {
                // (it was not a SELECT)
                int num_rows = mysql_affected_rows(mysql);  //返回update,insert,delete影响的行数
            }
            else // error
            {
                cout<<"Get result error: "<<mysql_error(mysql);
                return false;
            }
        }
        cout<<endl;cout<<endl;cout<<endl;
    }

    return true;

}

 

文件目录

 

 

结果如下

 

 

 

 

//

 



这篇关于c++ centos linux mysql通讯案例(安装和验证)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程