2012-04-20 14 views
7

解决方案:我已决定为有相同问题的人员提供一步一步的指导。我不知道这是否能够完全解决所有问题,但它确实可以让您编译我将提供的代码片段。如果有任何错误请评论,以便我可以修复它们。这是为了帮助大多数人失败的尝试,并坦率地说,整个过程应该被记录下来。很抱歉的长期职位:PMysql连接器/ C++ for windows 2010不能编译

STEP BY STEP FOR SETTING UP MYSQL CONNECTOR/C++ FOR 2010 MICROSOFT VISUAL C++ EXPRESS: 
FILES NEEDED: 
Windows x86 32 Bit ZIP: Connector/C++ -> http://dev.mysql.com/downloads/connector/cpp/ 
Windows X86 32 Bit ZIP: Connector/C -> http://dev.mysql.com/downloads/connector/c/ 
BOOST -> http://www.boost.org/users/download/ 

The reason why 62bit doesn't work is because you are using the express edition. After you have downloaded all those files, extract them. I keep mine in C (for ease of access). Now open up MVC++ 2010 EXPRESS. 
File -> New -> Project 
Win32 Console Application 
Next 
Check Empty project 
Finish 
Create your first .cpp. I call mine main.cpp 
Project -> Properties 
C/C++ -> General -> Additional Include Directories -> C:\mysql-connector-c++-noinstall-1.1.0-win32\mysql-connector-c++-noinstall-1.1.0-win32\include 
C/C++ -> General -> Additional Include Directories -> C:\mysql-connector-c++-noinstall-1.1.0-win32\mysql-connector-c++-noinstall-1.1.0-win32\include\cppconn 
C/C++ -> General -> Additional Include Directories -> C:\boost_1_49_0 
Linker -> General -> Additional Library Directories -> C:\mysql-connector-c++-noinstall-1.1.0-win32\mysql-connector-c++-noinstall-1.1.0-win32\lib 
Linker -> General -> Additional Library Directories -> C:\mysql-connector-c-noinstall-6.0.2-win32\mysql-connector-c-noinstall-6.0.2-win32\lib 
The reason why we downloaded the Connector/C is because it has two files we need: libmysql.lib and libmysql.dll 
Linker -> Input -> Additional Dependencies -> mysqlcppconn.lib 
Linker -> Input -> Additional Dependencies -> libmysql.lib 

If you change to Release, youll have to enter in the information again. 
In your main.cpp place the following code and build it: 

=

/* Copyright 2008, 2010, Oracle and/or its affiliates. All rights reserved. 

This program is free software; you can redistribute it and/or modify 
it under the terms of the GNU General Public License as published by 
the Free Software Foundation; version 2 of the License. 

There are special exceptions to the terms and conditions of the GPL 
as it is applied to this software. View the full text of the 
exception in file EXCEPTIONS-CONNECTOR-C++ in the directory of this 
software distribution. 

This program is distributed in the hope that it will be useful, 
but WITHOUT ANY WARRANTY; without even the implied warranty of 
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
GNU General Public License for more details. 

You should have received a copy of the GNU General Public License 
along with this program; if not, write to the Free Software 
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 
*/ 

/* Standard C++ includes */ 
#include <stdlib.h> 
#include <iostream> 

/* 
    Include directly the different 
    headers from cppconn/ and mysql_driver.h + mysql_util.h 
    (and mysql_connection.h). This will reduce your build time! 
*/ 
#include "mysql_connection.h" 

#include <cppconn/driver.h> 
#include <cppconn/exception.h> 
#include <cppconn/resultset.h> 
#include <cppconn/statement.h> 

using namespace std; 



int main(void) 
{ 
cout << endl; 
cout << "Running 'SELECT 'Hello World!' » AS _message'..." << endl; 

try { 
    sql::Driver *driver; 
    sql::Connection *con; 
    sql::Statement *stmt; 
    sql::ResultSet *res; 

    /* Create a connection */ 
    driver = get_driver_instance(); 
    con = driver->connect("tcp://127.0.0.1:3306", "YOUR USERNAME", "YOUR PASSWORD"); 
    /* Connect to the MySQL test database */ 
    con->setSchema("YOUR DATABASE"); 

    stmt = con->createStatement(); 
    res = stmt->executeQuery("SELECT 'Hello World!' AS _message"); 
    while (res->next()) { 
    cout << "\t... MySQL replies: "; 
    /* Access column data by alias or column name */ 
    cout << res->getString("_message") << endl; 
    cout << "\t... MySQL says it again: "; 
    /* Access column fata by numeric offset, 1 is the first column */ 
    cout << res->getString(1) << endl; 
    } 
    delete res; 
    delete stmt; 
    delete con; 

} catch (sql::SQLException &e) { 
    cout << "# ERR: SQLException in " << __FILE__; 
    cout << "(" << __FUNCTION__ << ") on line »" << __LINE__ << endl; 
    cout << "# ERR: " << e.what(); 
    cout << " (MySQL error code: " << e.getErrorCode(); 
    cout << ", SQLState: " << e.getSQLState() << ")" << endl; 
} 

cout << endl; 

int a;  // hangs 
cin >> a; 

return EXIT_SUCCESS; 
} 

你应该得到的错误中int8_t。双击位于:c:\ mysql-connector-C++ - noinstall-1.1.0-win32 \ mysql-connector-C++ - noinstall-1.1.0-win32 \ include \ cppconn \ config.h中的config.h。双击它以启动它。现在只需注释掉所有类型定义就应该是8.将libmysql.dll(在Connector/C中)和mysqlcppconn.dll(在Connector/C++中)放在编译器输出构建文件的文件夹内。完成保存并运行后。

旧文章不看

我按照本网站逐字步骤:http://blog.ulf-wendel.de/?p=215 但我仍然得到编译错误。我不是新来链接库,或包括目录,但我可以使用一个新的超级英雄,使用mysql连接器/ c + +与2010 express ...应用程序来创建一个简单的KISS示例。我一整天都在磨牙。 例来源:http://www.tidytutorials.com/2009/07/mysql-connector-c-example-windows-clexe.html(具有改变的数据库)

1>main.obj : error LNK2019: unresolved external symbol __imp__get_driver_instance referenced in function _main 

#include <stdlib.h> 
#include <iostream> 
using namespace std; 
#include <cppconn/driver.h> 
#include <cppconn/exception.h> 
#include <cppconn/resultset.h> 
#include <cppconn/statement.h> 
#include <cppconn/prepared_statement.h> 

int main(){ 

    sql::Driver *driver; 
    sql::Connection *con; 
    sql::Statement *stmt; 
    sql::ResultSet *res; 
    sql::PreparedStatement *pstmt; 

    try{ 
     driver = get_driver_instance(); 
     con = driver->connect("tcp://127.0.0.1:3306", "root", "root"); 
     con->setSchema("foxlogin"); 

     stmt = con->createStatement(); 
     stmt->execute("insert into example values(4,'four'),(5, 'five')"); 
     delete stmt; 

     pstmt = con->prepareStatement("select * from example"); 
     res = pstmt->executeQuery(); 
     while (res->next()) 
      std::cout<<res->getInt("id")<<" "<<res->getString("data")<<std::endl; 
     delete res; 
     delete pstmt; 

     pstmt = con->prepareStatement("delete from example where id=?"); 
     pstmt->setInt(1,4); 
     pstmt->executeUpdate(); 
     pstmt->setInt(1,5); 
     pstmt->executeUpdate(); 

     delete pstmt; 

     delete con;  
    }catch(sql::SQLException &e){ 
     std::cout<<e.what(); 
    } 

    int a;  // hang 
    cin >> a; 

    return 0; 
} 

UPDATE:我按照这也:http://forums.mysql.com/read.php?167,492097,492097#msg-492097我甚至用C++,和C的连接器(单独建造)以及与每个64两个测试沿,和32位版本(共4个测试)。我已经下载了提升并链接了它。我正在使用的新代码将发布在下面,它来自官方的mysql网站。这两个版本都会产生同样的错误。

/* Copyright 2008, 2010, Oracle and/or its affiliates. All rights reserved. 

This program is free software; you can redistribute it and/or modify 
it under the terms of the GNU General Public License as published by 
the Free Software Foundation; version 2 of the License. 

There are special exceptions to the terms and conditions of the GPL 
as it is applied to this software. View the full text of the 
exception in file EXCEPTIONS-CONNECTOR-C++ in the directory of this 
software distribution. 

This program is distributed in the hope that it will be useful, 
but WITHOUT ANY WARRANTY; without even the implied warranty of 
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
GNU General Public License for more details. 

You should have received a copy of the GNU General Public License 
along with this program; if not, write to the Free Software 
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 
*/ 

/* Standard C++ includes */ 
#include <stdlib.h> 
#include <iostream> 

/* 
    Include directly the different 
    headers from cppconn/ and mysql_driver.h + mysql_util.h 
    (and mysql_connection.h). This will reduce your build time! 
*/ 
#include "mysql_connection.h" 

#include <cppconn/driver.h> 
#include <cppconn/exception.h> 
#include <cppconn/resultset.h> 
#include <cppconn/statement.h> 

using namespace std; 

int main(void) 
{ 
cout << endl; 
cout << "Running 'SELECT 'Hello World!' » AS _message'..." << endl; 

try { 
    sql::Driver *driver; 
    sql::Connection *con; 
    sql::Statement *stmt; 
    sql::ResultSet *res; 

    /* Create a connection */ 
    driver = get_driver_instance(); 
    con = driver->connect("tcp://127.0.0.1:3306", "root", "root"); 
    /* Connect to the MySQL test database */ 
    con->setSchema("test"); 

    stmt = con->createStatement(); 
    res = stmt->executeQuery("SELECT 'Hello World!' AS _message"); 
    while (res->next()) { 
    cout << "\t... MySQL replies: "; 
    /* Access column data by alias or column name */ 
    cout << res->getString("_message") << endl; 
    cout << "\t... MySQL says it again: "; 
    /* Access column fata by numeric offset, 1 is the first column */ 
    cout << res->getString(1) << endl; 
    } 
    delete res; 
    delete stmt; 
    delete con; 

} catch (sql::SQLException &e) { 
    cout << "# ERR: SQLException in " << __FILE__; 
    cout << "(" << __FUNCTION__ << ") on line " » 
    << __LINE__ << endl; 
    cout << "# ERR: " << e.what(); 
    cout << " (MySQL error code: " << e.getErrorCode(); 
    cout << ", SQLState: " << e.getSQLState() << ")" << endl; 
} 

cout << endl; 

return EXIT_SUCCESS; 
} 

64测试版本错误:(我力包括32位编译错误的测试,如果你需要生病提供)

1>------ Build started: Project: erthwrthj, Configuration: Debug Win32 ------ 
    1> main.cpp 
    1>c:\mysql c++ 64\include\cppconn\sqlstring.h(36): warning C4251: 'sql::SQLString::realStr' : class 'std::basic_string<_Elem,_Traits,_Ax>' needs to have dll-interface to be used by clients of class 'sql::SQLString' 
    1>   with 
    1>   [ 
    1>    _Elem=char, 
    1>    _Traits=std::char_traits<char>, 
    1>    _Ax=std::allocator<char> 
    1>   ] 
    1>c:\mysql c++ 64\include\mysql_connection.h(156): warning C4251: 'sql::mysql::MySQL_Connection::proxy' : class 'boost::shared_ptr<T>' needs to have dll-interface to be used by clients of class 'sql::mysql::MySQL_Connection' 
    1>   with 
    1>   [ 
    1>    T=sql::mysql::NativeAPI::NativeConnectionWrapper 
    1>   ] 
    1>c:\mysql c++ 64\include\cppconn\exception.h(59): warning C4251: 'sql::SQLException::sql_state' : class 'std::basic_string<_Elem,_Traits,_Ax>' needs to have dll-interface to be used by clients of class 'sql::SQLException' 
    1>   with 
    1>   [ 
    1>    _Elem=char, 
    1>    _Traits=std::char_traits<char>, 
    1>    _Ax=std::allocator<char> 
    1>   ] 
    1>c:\mysql c++ 64\include\cppconn\config.h(60): error C2371: 'int8_t' : redefinition; different basic types 
    1>   c:\program files (x86)\microsoft visual studio 10.0\vc\include\stdint.h(17) : see declaration of 'int8_t' 
    1>c:\mysql c++ 64\include\cppconn\config.h(60): error C2371: 'int8_t' : redefinition; different basic types 
    1>   c:\program files (x86)\microsoft visual studio 10.0\vc\include\stdint.h(17) : see declaration of 'int8_t' 
    1>c:\users\null\documents\visual studio 2010\projects\erthwrthj\erthwrthj\main.cpp(74): error C2146: syntax error : missing ';' before identifier '»' 
    1>c:\users\null\documents\visual studio 2010\projects\erthwrthj\erthwrthj\main.cpp(74): error C2065: '»' : undeclared identifier 
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 

帮助将不胜感激!

更新2:view_findpost_ p _1474626" > http://www.dreamincode.net/forums/topic/253647-question-regarding-connecting-to-mysql-50-using-visual-studio-2010/ page_ 视图 _findpost_ p _1474626这家伙似乎认为这是因为由于上config.h中编译stdint.h config.h中的冲突存在于一个中int8_t #typedef,必须有这个解决方案。生病甚至可以使用任何其他可以连接到本地MySQL数据库的库(免费),但我真的很想知道如何帮助每个人。

+0

你尝试添加额外的库依赖于libmysql.lib?你可以从这里下载:http://dev.mysql.com/downloads/connector/c/ – 2012-04-20 07:38:56

+0

是的。这是我第二次尝试使其工作。而第二次我输了。 – user1328762 2012-04-20 08:00:34

+1

'62bit不能正常工作的原因'是否真的是从输出中复制粘贴的?我不会感到惊讶的是,有人在微软写了这样一个不正确的声明,但我想知道......为什么你的问题从“解决方案”开始?如果你有一个有效的答案,那么你应该为自己的问题写一个答案。 – 2013-11-21 01:28:05

回答

0

由于有很多关于C++和MySQL连接器的问题,它是雷尔我很难使它工作,我会回答这个问题,即使这是1年前提出的问题。

解决方案是很容易的,只是你需要做的事情是评论所有的typedef在cppcon/config.h中的文件