cpp mysql ubuntu

1.Install libmysqlcppconn-dev

sudo apt-get install libmysqlcppconn-dev

2.

//MySQLHelper.h 
#include <iostream>
#include <mysql_connection.h>
#include <mysql_driver.h>
#include <mysql_error.h>
#include <mysql_connection.h>
#include <cppconn/driver.h>
#include <cppconn/connection.h>
#include <cppconn/statement.h>
#include <cppconn/resultset.h>
#include <cppconn/exception.h>
#include <cppconn/prepared_statement.h>

using namespace std;
class MySQLHelper
{
public:
    void mySqlConnectDemo()
    {
        try
        {
            sql::Driver *driver;
            sql::Connection *conn;
            sql::Statement *stmt;
            sql::ResultSet *res;
            sql::ResultSetMetaData *resMeta;
            sql::PreparedStatement *pstmt;

            driver = get_driver_instance();
            conn = driver->connect("tcp://127.0.0.1:3306", "username", "password");
            conn->setSchema("db");

            stmt = conn->createStatement();

            pstmt = conn->prepareStatement("select * from Book");
            res = stmt->executeQuery("select * from Book");
            resMeta=res->getMetaData();
            int fieldsCount=resMeta->getColumnCount();
            while (res->next())
            {
                cout<<res->getInt(1)<<","<<res->getInt64(2)<<","<<res->getString(3)<<","<<res->getString(4)<<","
                <<res->getString(5)<<","<<res->getString(6)<<","<<res->getString(7);
                cout<<endl<<endl;
            }
        }
        catch (const std::exception &e)
        {
            std::cerr << e.what() << 'n';
        }
    }
};

 

2.

//main.cpp
#include "Model/MySQLHeler.h"

void mySqlHelperDemo()
{
    MySQLHelper mh;
    mh.mySqlConnectDemo();
}

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

 

Compile

g++ -g -std=c++2a -I. *.cpp ./Model/*.cpp -o h1 -ljsoncpp -luuid -lmysqlcppconn

Run

cpp mysql ubuntu

 

 

 

In mysql select result 

cpp mysql ubuntu

 

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

欢迎关注

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

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

    cpp mysql ubuntu

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

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

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

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

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

相关推荐