首页 > 数据库 >cpp mysql ubuntu

cpp mysql ubuntu

时间:2022-12-03 22:23:28浏览次数:38  
标签:getString res sql conn mysql cpp include 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

 

 

 

In mysql select result 

 

标签:getString,res,sql,conn,mysql,cpp,include,ubuntu
From: https://www.cnblogs.com/Fred1987/p/16948911.html

相关文章

  • mysql--约束
        外键约束:altertableempaddconstraintfk_emp_dept_idforeignkey(dept_id)referencesdept(id);添加主键altertableempdropforeignkeyfk_em......
  • Ubuntu18.04安装docker
    一、安装1.更新源sudoapt-getupdate2.安装依赖:sudoapt-getinstallapt-transport-httpsca-certificatescurlgnupg2software-properties-common3.信任Do......
  • [MySQL] 索引失效的情况
    1.查询条件中有or,即使有部分条件带索引也会失效2.like查询是以%开头3.如果列类型是字符串,那在查询条件中需要将数据用引号引用起来,否则不走索引4.索引列上参与计算......
  • cpp list files end with suffix and list them respectively
    #include<chrono>#include<ctime>#include<dirent.h>#include<fstream>#include<iostream>#include<thread>#include<unistd.h>#include<uuid/uuid.h>#in......
  • cpp template class defined in .h files and implemented in cpp files
    //MathHelper.h#ifndef_MathHelper_H#define_MathHelper_H#include<iostream>#include<random>#include<stdio.h>usingnamespacestd;template<classT>......
  • MySQL 5.7 主从复制 GTID
    GTID中slave端的binlog是必须开启的,目的是记录执行过的GTID主库#开启gtidlog-bin=mysql-binexpire_logs_days=5binlog_format=mixedserver-id=1gtid_mode=onenforce_g......
  • Cpp serialize class in vector collection and define the serialize method in clas
    //Model/Book.cpp#include<iostream>#include<jsoncpp/json/json.h>usingnamespacestd;classBook{public:intIdx;doubleId;char*Abstract;......
  • MySQL数据库之事务
    一、事务的概念事务是一种机制、一个操作序列,包含了一组数据库操作命令,并且把所有的命令作为一个整体一起向系统提交或撤销操作请求,即这一组数据库命令要么都执行,要么......
  • sql递归查询-mysql8为例
    总体描述数据准表通过CTE实现,当前版本是mariadb8.0.31,于mysql8相当。数据表(也就是原表):select*fromrecurrenceaidparent_id全球null中国全球......
  • centos8 手动卸载mysql
    首先得停止mysql的服务,然后查找mysql文件用find/-namemysql   rm-rf删掉所有的mysql文件删掉后删除配置文件rm-rf/etc/my.cofrm-rf/etc/init.d/mysql......