连接mysql5.7 使用 mysql-connector-c++-1.1.13

#include 
#include 
#include 
#include 
#include "mysql_driver.h"
#include "mysql_connection.h"
#include "cppconn/driver.h"
#include "cppconn/statement.h"
#include "cppconn/prepared_statement.h"
#include "cppconn/metadata.h"
#include "cppconn/exception.h"
 
using namespace std;
using namespace sql;
 //g++ -o my aa.cpp -std=c++14   -lssl -lmysqlcppconn  -I/usr/local/include -I./include -L./lib
 //install_name_tool -change libmysqlcppconn.7.1.1.13.dylib ./lib/libmysqlcppconn.7.1.1.13.dylib ./my
//install_name_tool -chang old new ./my
//otool -L ./my
 //g++ -o my aa.cpp -std=c++14   -lssl ./lib/libmysqlcppconn-static.a  -I/usr/local/include -I./include -L./lib
int main()
{
    sql::mysql::MySQL_Driver *driver = 0;
    sql::Connection *conn = 0;
 
    try
    {
        driver = sql::mysql::get_mysql_driver_instance();
        conn = driver->connect("tcp://localhost:3306/aa", "root", "123456");
        cout << "连接成功" << endl;
    }
    catch (...)
    {
        cout << "连接失败" << endl;
    }
    sql::Statement* stat = conn->createStatement();
    stat->execute("set names 'utf8'");
    ResultSet *res;
    res = stat->executeQuery("SELECT * FROM commpp");
    cout<<"rows:"<rowsCount()<next())
    {
        cout <getString("cmtid") << endl;
        cout << "BOOKNAME:" << res->getString("cmtdesc") << endl;
        //cout << "    SIZE:" << res->getString("size") << endl;
        a++;
    }
    if (conn != 0)
    {
        delete conn;
    }
  return 0;
}

mysqlx  好像不支持 mysql5.7  mysql-connector-c++-1.1.13-macos10.14-x86-64bit.tar.gz  mac 使用1.1.13

 g++ -o my aa.cpp -std=c++14   -lssl -lmysqlcppconn  -I/usr/local/include -I./include -L./lib

 

otool -l ./my   看看程序找动态库路径

替换动态库路径

install_name_tool -change libmysqlcppconn.7.1.1.13.dylib ./lib/libmysqlcppconn.7.1.1.13.dylib ./my

如果用的是 mysql-connector-c++8.0
install_name_tool -change @rpath/libmysqlcppconn.7.dylib ./lib64/libmysqlcppconn.7.dylib ./my

 

你可能感兴趣的:(c++)