2012-04-20 24 views
7

GIẢI PHÁP: Tôi đã quyết định thực hiện hướng dẫn từng bước cho những người có cùng vấn đề. Tôi không biết nếu điều này hoàn toàn sửa chữa tất cả mọi thứ nhưng nó cho phép bạn biên dịch đoạn mã tôi sẽ cung cấp. Nếu có bất kỳ lỗi nào, hãy bình luận để tôi có thể sửa chúng. Điều này nhằm mục đích giúp đỡ những người hầu như đã từ bỏ những nỗ lực thất bại, và nói thẳng thắn, toàn bộ quá trình nên được ghi chép lại. Xin lỗi cho đăng bài dài: PKết nối Mysql/C++ cho windows 2010 express không biên dịch

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; 
} 

Bạn sẽ nhận được lỗi cho int8_t. Nhấn đúp vào config.h tại: c: \ mysql-connector-C++ - noinstall-1.1.0-win32 \ mysql-connector-C++ - noinstall-1.1.0-win32 \ include \ cppconn \ config.h. Nhấp đúp vào nó để đưa nó lên. Bây giờ chỉ cần bình luận ra tất cả các typedefs chỉ nên có 8. Đặt libmysql.dll (bên trong Connector/C) và mysqlcppconn.dll (bên trong Connector/C++) bên trong thư mục trình biên dịch xuất ra bản build của bạn. Khi hoàn thành lưu và chạy.

bài viết cũ DONT XEM

Tôi đã làm theo các bước trên trang web này đúng nguyên văn: http://blog.ulf-wendel.de/?p=215 Nhưng tôi vẫn nhận được một lỗi biên dịch. Tôi không phải mới để liên kết các thư viện, hoặc bao gồm các thư mục, nhưng tôi có thể sử dụng một siêu anh hùng mới, người đã tạo các ứng dụng với trình kết nối mysql/C++ với 2010 express ... để tạo một ví dụ KISS đơn giản. Tôi đã nghiến răng cả ngày. Ví dụ Nguồn: http://www.tidytutorials.com/2009/07/mysql-connector-c-example-windows-clexe.html (với thay đổi cơ sở dữ liệu)

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: Tôi đã theo này cũng: http://forums.mysql.com/read.php?167,492097,492097#msg-492097 Tôi thậm chí còn sử dụng C++, và kết nối C (riêng biệt được xây dựng) cùng với cả hai thử nghiệm với mỗi 64 và 32 bit phiên bản (tổng cộng 4 bài kiểm tra). Tôi đã tải về tăng cường và liên kết nó. Mã mới tôi đang sử dụng Tôi sẽ đăng bài dưới đây đến từ trang web mysql chính thức. Cả hai bản dựng đều tạo ra lỗi SAME.

/* 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; 
} 

LỖI BUILD CHO 64 TEST: (Tôi dint bao gồm các bài kiểm tra lỗi 32bit build, nếu bạn cần Ill cung cấp)

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 ========== 

Trợ giúp sẽ được đánh giá!

CẬP NHẬT 2: view_findpost_ p _1474626 "> http://www.dreamincode.net/forums/topic/253647-question-regarding-connecting-to-mysql-50-using-visual-studio-2010/ page_ xem _findpost_ p _1474626 Anh chàng này dường như nghĩ rằng đó là vì config.h xung đột với stdint.h khi biên dịch vì trong config.h có một #typedef cho int8_t. Phải có giải pháp cho điều này. thậm chí có bất kỳ thư viện nào khác (miễn phí) có thể kết nối với một cơ sở dữ liệu MySQL cục bộ, nhưng tôi thực sự háo hức tìm ra điều này để giúp mọi người.

+0

Bạn đã cố gắng thêm một sự phụ thuộc thư viện bổ sung cho libmysql.lib? Bạn có thể tải xuống từ đây: http://dev.mysql.com/downloads/connector/c/ –

+0

Có.Đây là liên doanh thứ hai của tôi đang cố gắng làm cho nó hoạt động. Và lần thứ hai tôi đã thua. – user1328762

+1

'Lý do tại sao 62bit không hoạt động' Đó thực sự là một bản sao và dán từ đầu ra? Tôi sẽ không ngạc nhiên khi một người nào đó ở Microsoft viết một tuyên bố không chính xác như vậy, nhưng tôi đã tự hỏi ... Tại sao CÂU HỎI của bạn bắt đầu với "GIẢI PHÁP"? Nếu bạn có câu trả lời, bạn nên viết câu trả lời cho câu hỏi của mình. –

Trả lời

0

Vì có rất nhiều câu hỏi về đầu nối C++ và MySQL. reall y khó để làm cho nó hoạt động, tôi sẽ trả lời câu hỏi này ngay cả điều này đã được hỏi như 1 năm trước.

Giải pháp là khá dễ dàng, chỉ có điều bạn cần làm là nhận xét tất cả của typedef trong cppcon/file config.h

Các vấn đề liên quan