zoukankan      html  css  js  c++  java
  • 编译和使用 MySQL C++ Connector

    记录编译 mysql C and C++ connector 和简单访问数据库.

    环境: vs2012,  mysql 5.6.13, 基于x64

    0. 软件包

    mysql http://dev.mysql.com/downloads/mysql/ 

    c connector  http://dev.mysql.com/downloads/connector/c/

    c++ connector依赖C connector, mysql的binary里面有编译好的C connector lib, 如果你用的编译器和mysql 一样, 那么不用重新编译

    c++ connector http://dev.mysql.com/downloads/connector/cpp/

    boost http://sourceforge.net/projects/boost/files/boost/1.54.0/

    c++ Connector 依赖boost. 本来还尝试过vs2013, 但是boost1.54在vs2013 preview下编译有问题, 我没有试低版本的boost.

    cmake http://www.cmake.org/cmake/resources/software.html

    mysql的project是基于cmake. cmake可以生成vc project, 使用起来挺方便


    1. 环境bat vs2012x64env.bat

    call "C:Program Files (x86)Microsoft Visual Studio 11.0VCvcvarsall.bat" amd64
    set PATH=%PATH%;D:mybincmake-2.8.11.1-win32-x86in
    set MYSQL_DIR=D:mybinmysql-5.6.13-winx64
    set BOOST_ROOT=D:mybinoost_1_54_0
    cmd /K

    2. Generate vc project

    C connector
    cmake -G "Visual Studio 11 Win64"
    build project libmysql, 只需要一个lib, 不用编译所有, debug模式下, 自带trace


    C++ connector

    如果想要打开trace, 加-DMYSQLCPPCONN_TRACE_ENABLE:BOOL=1, 实际还没有结束, 大概是cmake生成project的错误, 需要手动添加预定义宏 CPPCONN_TRACE_ENABLED 
    cmake -DMYSQLCPPCONN_TRACE_ENABLE:BOOL=1 -G "Visual Studio 11 Win64"
    build project mysqlcppconn

    把头文件copy在mysql下, 方便些, 不是必须
    copy D:mybinmysql-connector-c++-1.1.3cppconn  D:mybinmysql-5.6.13-winx64includecppconn


    3.Demo, 贴代码了

    #include <iostream>

    #include <memory>
    #include <string>
    #include <cppconndriver.h>
    #include <cppconnexception.h>
    #include <cppconn esultset.h>
    #include <cppconnstatement.h>
    #include <cppconnprepared_statement.h>

    static const char*  g_sqltrance = "";//"d:t:O,client.trace";
    static const int    g_enable_debug_trace = 0;

    int main()
    try
    {

        std::cout << "connect db start ... ";
        auto driver = get_driver_instance();

        std::unique_ptr<sql::Connection> con(driver->connect("tcp://127.0.0.1:3306", "root", ""));
        con->setClientOption("libmysql_debug",  g_sqltrance);
        con->setClientOption("clientTrace",    &g_enable_debug_trace);
        con->setSchema("test");

        // create table
        std::unique_ptr<sql::Statement> stmt(con->createStatement());
        stmt->execute("drop table if exists test");
        stmt->execute("create table test(id int, label char(1))");

        // insert
        stmt->execute("insert into test(id, label) values (1, 'a')");
        stmt->execute("insert into test(id, label) values (2, 'b')");
        stmt->execute("insert into test(id, label) values (3, 'c')");

        // update
        stmt->executeUpdate("update test set label='e' where id=2");

        // prepared statement
        std::unique_ptr<sql::PreparedStatement> prep_stmt(con->prepareStatement("insert into test(id, label) values(?, ?)"));
        size_t n = 5;
        while (n++ < 30)
        {
            prep_stmt->setInt(1, n);
            prep_stmt->setString(2, sql::SQLString(std::string(1, 'A' + n)));
            prep_stmt->execute();
        }

        // query
        std::unique_ptr<sql::ResultSet> res(stmt->executeQuery("select id, label from test order by id asc"));
        while (res->next())
        {
            std::cout << "id = " << res->getInt(1) << "  label = " << res->getString("label") << " ";
        }

        return 0;
    }
    catch (sql::SQLException & e)
    {
        std::cout << "SQLException :    "
                  << e.what() 
                  << "    ErrorCode " << e.getErrorCode() << " "
                  << "    SQLState " << e.getSQLState() << " ";
    }
    catch (...)
    {
        std::cout << "uncatch exception ";
    }

    4. 如果想导入sample数据库

    http://dev.mysql.com/doc/index-other.html
    download world database InnoDB version
    mysql -u root
    create database world;
    use world;
    source c:world_innodb.sql
    show tables;


  • 相关阅读:
    APP测试-流量测试
    APP测试-流畅度测试
    APP测试-耗电分析
    工具安装-Homebrew
    工具安装-go for Mac
    APP测试-耗电量测试
    APP测试-CPU测试
    APP测试-内存测试
    APP测试-monkey
    APP测试-adb命令
  • 原文地址:https://www.cnblogs.com/keanuyaoo/p/3253828.html
Copyright © 2011-2022 走看看