zoukankan      html  css  js  c++  java
  • VC++2010配置使用MySQL5.6

     

     

    0、前提

        安装后的文件概览

        

     

     

        编译器:  VC++2010

     

        MySQL版本:MySQL5.6.19 for win64     

        

     

        Connector版本:connector  c++  1.1.3

        

     

     

     

    在VS2010下配置使用MySQL

     

    1、配置头文件

        项目属性--VC++目录--包含目录

        

     

     

    2、配置库文件

        在connector c++ 1.1.3lib目录下有两个目录:debug目录 和 opt目录

        

     

     

        libdebug目录

        

     

     

     

        libopt目录

        

     

        由于有debug目录,所以猜测opt目录可能是类似release目录的优化(optimize)后的文件,因此在VC++中使用时在Debug下使用debug目录下的库文件,在Release模式下使用opt目录下的库目录。

     

        eg.

        #ifdef  _DEBUG

        #pragma   comment(lib, "debug下的mysqlcppconn.lib")

        #pragma   comment(lib, "debug下的mysqlcppconn-static.lib")

        #else 

        #pragma   comment(lib, "opt下的mysqlcppconn.lib")

        #pragma   comment(lib, "opt下的mysqlcppconn-static.lib")

        #endif

     

        另外,在Debug或Release模式下将debug或opt目录下的mysqlcppcon.dll拷贝到项目目录下或system32目录下。  将 MySQLMySQL Server5.6lib目录下的libmysql.dll拷贝到项目目录下或system32目录下。

        

     

        

    3、配置项目

        由于该版本的MySQL是64位的,因此使用该MySQL的connector的项目必须被配置为X64类型的。 否则会有链接错误! 这一点要注意!

        

     

    4、Demo

        数据库:db_1220, 表:tbl_user,  MySQL服务器:本地的localhost

        

     

        

    #include "stdafx.h"
    #include <Windows.h>
    #include <iostream>
    #include <mysql_connection.h>
    #include <cppconn/driver.h>
    #include <cppconn/exception.h>
    #include <cppconn/resultset.h>
    #include <cppconn/statement.h>
    #pragma warning(disable:4251)
    
    
    #ifdef   _DEBUG
    #pragma comment(lib, "D:\Program Files\MySQL\Connector C++ 1.1.3\lib\debug\mysqlcppconn-static.lib")
    #pragma comment(lib, "D:\Program Files\MySQL\Connector C++ 1.1.3\lib\debug\mysqlcppconn.lib")
    #else
    #pragma comment(lib, "D:\Program Files\MySQL\Connector C++ 1.1.3\lib\opt\mysqlcppconn-static.lib")
    #pragma comment(lib, "D:\Program Files\MySQL\Connector C++ 1.1.3\lib\opt\mysqlcppconn.lib")
    #endif
    
    
    
    using namespace  std;
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        sql::Driver     *driver = NULL;
        sql::Connection *con    = NULL;
        sql::Statement  *stmt   = NULL;
        sql::ResultSet  *res    = NULL;
    
        sql::SQLString strHost("localhost");
        sql::SQLString strUser("root");
        sql::SQLString strPwd("XXXXXXX");
        sql::SQLString strSchema("db_1220");
        sql::SQLString strQuery("select * from tbl_user");
    
        
        try
        {
    
            driver = get_driver_instance();
    
            con = driver->connect(strHost, strUser, strPwd);
            con->setSchema(strSchema);
    
            stmt = con->createStatement();
            res = stmt->executeQuery(strQuery);
            sql::ResultSetMetaData* pMetaData = res->getMetaData();
    
            cout << endl;
    
            cout << "Results have " << res->rowsCount() << " rows" << endl << endl;
    
            while(res->next())
            {
                //get data by column name
                cout << res->getInt("id")
                     << "   "
                     << res->getString("name").c_str()   //sql::SQLString没有重载<<操作符,因此不能直接cout<<res->getString("name")
                     << "   "
                     << res->getString("password").c_str()
                     << endl;
    
                //get data by column index
                cout << res->getInt(1)
                     << "   "
                     << res->getString(2).c_str()
                     << "   "
                     << res->getString(3).c_str()
                     << endl;
            }
    
        }
        catch (sql::SQLException& e)
        {
            cerr << endl << e.what() << endl;
        }    
        catch (...)
        {
            cerr << endl << "some exception happeded" << endl;
        }
    
    
        if (NULL != res)
            delete res;
        
        if (NULL != stmt)
            delete stmt;
    
        if (NULL != con)
            delete con;
    
    
        cout << endl << endl;
        
        return 0;
    }

     

        

     

    运行结果:

     

     

     5、补充

         如果在编译过程中报错找不到类似 “<boost/variant.hpp>”这样的错误信息,则是需要boost库支持,下载boost库配置一下即可。

      

        

     

  • 相关阅读:
    Java自学第8期——多线程
    Java自学第7期——异常(Exception)
    Java自学第6期——Collection、Map、迭代器、泛型、可变参数、集合工具类、集合数据结构、Debug
    java自学第5期——Object、Date、Calender、System、StringBuilder、基本类型包装类
    下载com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
    博客园主题SimpleMemory详细配置项
    banner自用图床
    java自学第4期——:Scanner类、匿名对象介绍、Random类、ArrayList集合、标准类格式、String类、static静态、Arrays工具类、Math类(1)
    java自学第3期——继承、多态、接口、抽象类、final关键字、权限修饰符、内部类
    数组,哈希表,字典
  • 原文地址:https://www.cnblogs.com/cuish/p/4175592.html
Copyright © 2011-2022 走看看