C++连接mysql数据库代码示例

目录结构

官方驱动方式
#include "mysql.h"
#include <iostream>
using namespace std;

int main()
{
    MYSQL mysql;
    MYSQL_RES *res;
    MYSQL_ROW row;
    mysql_init(&mysql);
    mysql_options(&mysql, MYSQL_SET_CHARSET_NAME, "utf8");
    if (NULL == mysql_real_connect(&mysql, "localhost", "root", "xxxx", "xxxxx", 3306, NULL, 0))
    {
        cout << "mysql connect faild." << endl;
        exit(-1);
    }
    int ret = mysql_query(&mysql, "select * from mysql_test");

    cout << "ret : " << ret << endl;
    res = mysql_store_result(&mysql);

    while (row = mysql_fetch_row(res))
    {
        cout << "ID : " << row[0] << endl;
        cout << "username : " << row[1] << endl;
        cout << "password : " << row[2] << endl;
    }

    mysql_free_result(res);
    mysql_close(&mysql);
    system("pause");
    return 0;
}
编译链接
g++ .\src\mysqltest.cpp -I D:\application\javaweb\install\mysql\include -L .\bin\ -llibmysql -o test.exe

D:\application\javaweb\install\mysql\include 为mysql安装头文件目录
bin目录放有官方库
libmysql.dll
ODBC 连接方式


#include <windows.h>
#include <sql.h>
#include <sqlext.h>
#include <iostream>

#pragma comment(lib, "odbc32.lib");
int main()
{
    SQLHENV henv;                         // ODBC 环境
    SQLRETURN sqlrt = SQLAllocEnv(&henv); //申请初始化环境
    if (SQL_SUCCESS != sqlrt && SQL_SUCCESS_WITH_INFO != sqlrt)
    {
        std::cout << "初始化ODBC环境失败" << std::endl;
        return -1;
    }

    SQLHDBC hdbc; //数据库连接
    sqlrt = SQLAllocConnect(henv, &hdbc);

    if (SQL_SUCCESS != sqlrt && SQL_SUCCESS_WITH_INFO != sqlrt)
    {
        std::cout << "申请ODBC连接失败" << std::endl;
        SQLFreeConnect(hdbc); //释放连接
        SQLFreeEnv(henv);     //释放申请环境
        return -1;
    }

    sqlrt = SQLConnectA(hdbc, (SQLCHAR *)"odbcmysql", SQL_NTS, (SQLCHAR *)"root", 20, (SQLCHAR *)"xxxx", 10);
    if (SQL_SUCCESS != sqlrt && SQL_SUCCESS_WITH_INFO != sqlrt)
    {
        std::cout << "ODBC连接数据库失败" << std::endl;
        SQLFreeConnect(hdbc); //释放连接
        SQLFreeEnv(henv);     //释放申请环境
        return -1;
    }

    SQLHSTMT hstmt;             //查询结果存储空间
    SQLAllocStmt(hdbc, &hstmt); //申请空间

    sqlrt = SQLExecDirectA(hstmt, (SQLCHAR *)"select * from mysql_test", SQL_NTS);

    if (SQL_SUCCESS != sqlrt && SQL_SUCCESS_WITH_INFO != sqlrt)
    {
        std::cout << "ODBC执行SQL失败" << std::endl;
        SQLFreeStmt(hstmt, SQL_DROP); //释放存储空间
        SQLDisconnect(hdbc);          //断开连接
        SQLFreeConnect(hdbc);         //释放连接
        SQLFreeEnv(henv);             //释放申请环境
        return -1;
    }

    int id;
    SQLCHAR username[20], password[20];
    SQLLEN iLen, uLen, pLen;

    for (sqlrt = SQLFetch(hstmt); sqlrt == SQL_SUCCESS; sqlrt = SQLFetch(hstmt))
    {
        SQLGetData(hstmt, 1, SQL_INTEGER, &id, sizeof(id), &iLen);
        SQLGetData(hstmt, 2, SQL_C_CHAR, username, sizeof(username), &uLen);
        SQLGetData(hstmt, 3, SQL_C_CHAR, password, sizeof(password), &pLen);
        std::cout << "ID : " << id << " length : " << iLen << std::endl;
        std::cout << "USERNAME : " << username << " length : " << uLen << std::endl;
        std::cout << "PASSWORD : " << password << " length : " << pLen << std::endl;
    }

    SQLFreeStmt(hstmt, SQL_DROP); //释放存储空间
    SQLDisconnect(hdbc);          //断开连接
    SQLFreeConnect(hdbc);         //释放连接
    SQLFreeEnv(henv);             //释放申请环境

    system("pause");
    return 0;
}
ODBC 环境配置

添加odbc 数据源

编译链接
g++ .\src\odbcmysql.cpp -lodbc32 -o test.exe