mysql connector c++ (vc2010)

  这几天一直在尝试c++连接mysql,尝试了mysql c api以及mysql connector c++ 这2种方法(还有一种方法是使用ado,但是撸主比较懒,还是喜欢比较直接点的方法),然而mysql c api 编译通过但是运行怎么都不行,所以换成了mysql connector c++ 这种方式。接下来我将会记录我在配置vc2010的过程

  0x00  环境

    IDE:vc2010

    c++库:boost(使用mysql connector c++需要先配置boost库),mysql connector c++ 1.1.5

    数据库:mysql5.6

  0x01  安装boost库以及mysql connector c++

    首先下载boost库以及mysql connector c++(这2项都能再各自的官网上下到,就不提供链接了)

    安装boost库,下载完boost-1.58.zip ,解压缩后运行bootstrap.bat,运行完成后会多出b2.exe以及bjam.exe这2个可运行程序,这里我们只需接着运行bjam.exe(这个程序运行的时间会比较长,可以看一集银魂的说^_^),结束后你可以把总的文件贴到你需要的文件夹内。

    至于mysql connector c++,我直接下载的是.msi安装文件,安装即可,默认目录为C:\Program Files\MySQL\MySQL Connector C++ 1.1.5

    至此需要的文件已安装完毕,接下来就是配置vc2010了。

  0x02  配置vc2010

    1.首先创建一个空的工程,并添加一个cpp文件。

    2.为当前工程添加目录

      找到配置选项页:

        首先我们需要确认你的mysql版本以及mysql connector c++的版本是32位还是64位,如果是均为64位,我们需要将vc2010的平台设置为64位的,不然 编译无法通过。

        Project-->Properties选项页中有Pllatform这个选项,默认为一开始创建的32位,点击Configuration Manager修改为64位

        Project-->Properties-->C/C++  选择General 里的Additional Include Directories   添加mysql connector c++ 的include 文件夹以及boost的文件夹

        

        Project-->Properties-->Linker  选择General 里的Additional Library Directories   添加mysql connector c++ 的lib 文件夹以及boost的libs文件夹

        

        Project-->Properties-->Linker  选择Input 里的Additional Dependencies  添加mysqlcppconn.lib

        

        至此我们已经完成了vc2010的配置

   0x03  验证

    接下来就是验证环境是否已成功配置,我们可以写入以下的测试代码(首先要确保mysql有测试的database)

#include <iostream>
#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;

int main()
{
    sql::mysql::MySQL_Driver *my_driver = 0;
    sql::Connection *connect = 0;

    try//尝试连接testdb数据库
    {
        my_driver = sql::mysql::get_mysql_driver_instance();
        connect = my_driver->connect("tcp://localhost:3306/testdb", "root", "12345678");
        cout << "connect success!" << endl;
    }
    catch (exception e)//连接失败
    {
        cout << "connect fail!" << endl;
    }
    //取出示例数据
    sql::Statement* stat = connect->createStatement();
    stat->execute("set names ‘gbk‘");
    ResultSet *res;
    res = stat->executeQuery("SELECT * FROM user");
    while (res->next())
    {
        cout << "ID:" << res->getString("id") << endl;
        cout << "NAME:" << res->getString("name") << endl;
    }
    if (connect != 0)//释放连接
    {
        delete connect;
    }
    getchar();
}

  0x04 结尾

    mysql c api估计也是版本问题,编译明明能通过,however, there is no egg use。所以遇到实在无法解决的问题可以尝试着换另一种方法(技术太渣,ouduok)

时间: 2024-08-24 13:40:25

mysql connector c++ (vc2010)的相关文章

MySQL Connector/J 6.x jdbc.properties 配置, mysql-connector-java-6.0.4.jar 异常

报错信息 Caused by: com.mysql.cj.core.exceptions.InvalidConnectionAttributeException: The server time zone value '?й???????' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezon

Using MySQL Connector .NET 6.6.4 with Entity Framework 5

I had been waiting for the latest MySQL connector for .NET to come out so I can move on to the new and sleek Visual Studio 2012 for my projects. Found out connector 6.6.4 has been out which can work with VS2012 so I happily install it. I start a bran

mysql.connector操作mysql的blob值

This tutorial shows you how to work with MySQL BLOB data in Python, with examples of updating and reading BLOB data. The  authors table has a column named  photo whose data type is BLOB. We will read data from a picture file and update to the photo c

Installing MySQL Connector/Python using pip v1.5

The latest pip versions will fail on you when the packages it needs to install are not hosted on PyPI . When you try to install MySQL Connector/Python the usually way, you get following message is: shell> pip install mysql-connector-python Could not

MySQL Connector/Python 开发者指南

本文翻译自:https://dev.mysql.com/doc/connector-python/en/ 摘要 这篇手册描述了怎么来安装和配置 MySQL Connector/Python,MySQL Connector/Python 是 Python 和 MySQL 服务进行通信的一个独立驱动程序,本篇手册还将阐述如何利用它来开发数据库应用程序. 获取 Connector/Python 的每一次发布版本的改动细节,请参阅 MySQL Connector/Python Release Notes

mysql.connector 事务总结

mysql.connector事务总结: connection.autocommit = 0 (默认值) 事务处理 使用 connection.commit()方法 #!/usr/bin/env python # -*- coding:utf-8 -*- '''mysql.connector事务总结: connection.autocommit = 0 (默认值) 事务处理 使用 connection.commit()方法 分析: 智能commit状态: connection.autocommi

Snippet: Fetching results after calling stored procedures using MySQL Connector/Python

https://geert.vanderkelen.org/2014/results-after-procedure-call/ Problem Using MySQL Connector/Python, you are calling a stored procedure which is also selecting data and you would like to fetch the rows of the result. Solution For this example we cr

Visual Studio 2015编译64位MySQL Connector/C++

目前MySQL Connector/C++的binary版本最高只支持VS2008,VS2015需要下载源码自行编译. 尽管MySQL手册提供了信息,但在编译过程中还是有不少细节需要注意. CMAKE 到官网下载最新的稳定版本 把bin目录添加到环境变量PATH中 Boost 同样到官网下载最新的稳定版本 MySQL客户端库 MySQL客户端库头文件在MySQL目录下的include目录中 是的,编译connector还需要下载一个MySQL Server 添加环境变量MYSQL_DIR,值为M

Developing DataBase Applications Using MySQL Connector/C++ 中文文本

Developing DataBase Applications Using MySQL Connector/C++ 中文文本 ? by grayondream 翻译自mysql Connector C++帮助文档[http://download.csdn.net/detail/midle110/4931166] ? 本教程将利用从MySQL数据库中链接,插入和检索数据的简单示例向您展示构建和安装MySQL Connector / C ++驱动程序的基本步骤.因为本文的重点是使用C++ Conn