C++ mysql

1.Install C++ connector

sudo apt install libmysqlcppconn-dev

2.

#include <iostream>
#include <cppconn/driver.h> 
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/exception.h>

using namespace std;

void mySQL65();

int main(int args, char **argv)
{
    mySQL65();
}

void mySQL65()
{
    try
    {
        sql::Driver *driver;
        sql::Connection *conn;
        sql::Statement *stmt;
        sql::ResultSet *res;

        driver = get_driver_instance();
        conn = driver->connect("tcp://127.0.0.1:3306", "root", "Root0001!");
        conn->setSchema("myDB");

        stmt = conn->createStatement();
        res = stmt->executeQuery("SELECT 'Hello World!' AS _message"); // replace with your statement
        while (res->next())
        {
            cout << "t... MySQL replies: ";
            /* Access column data by alias or column name */
            cout << res->getString("_message") << endl;
            cout << "t... MySQL says it again: ";
            /* Access column fata by numeric offset, 1 is the first column */
            cout << res->getString(1) << endl;
        }
        delete res;
        delete stmt;
        delete conn;
    }
    catch (sql::SQLException &e)
    {
        cout << "# ERR: SQLException in " << __FILE__;
        cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
        cout << "# ERR: " << e.what();
        cout << " (MySQL error code: " << e.getErrorCode();
        cout << ", SQLState: " << e.getSQLState() << " )" << endl;
    }
}

 

3.Compile via g++ and inclue mysql connector ,  -I/usr/include/cppconn       -L/usr/lib -lmysqlcppconn

g++ -g -std=c++2a -I. -Wall -I/usr/include/cppconn  *.cpp ./Model/*.cpp -o h1 -luuid -lpthread -L/usr/lib -lmysqlcppconn

4.Run the compiled program

C++ mysql

 

原文链接: https://www.cnblogs.com/Fred1987/p/16268975.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    C++ mysql

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/401491

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年4月19日 上午9:16
下一篇 2023年4月19日 上午9:16

相关推荐