2012-04-13 42 views
0

你好,我剛剛開始使用C++ MySQL連接的器件的應用 發佈| WIN3MySQL連接/ C++的示例代碼是不工作

我的錯誤:

警告1個警告C4627: '#包括' :預編譯頭用C看 時跳過:跳過預編譯頭使用C++尋找 時:\ Documents和Settings ...

警告2警告C4627: '#包括' \的Documents and Settings ...

錯誤3致命錯誤C1083:無法打開包含文件: '升壓/ variant.hpp':沒有這樣的文件或目錄C:\ PROGRAM 文件\ MySQL的\ MySQL連接C++ 1.1.0 \包括\ cppconn \ connection.h 29 lacznik

#include <cstdlib> 
#include <iostream> 
#include "stdafx.h" 
#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; 
} 
+2

你可以正確縮進你的代碼嗎?我不想浪費我的志願時間精神上跟蹤示波器。 – 2012-04-13 10:31:03

+0

此代碼來自http://dev.mysql.com/doc/refman/5.1/en/connector-cpp-examples-complete-example-1.html我不確定是否依賴y庫。這是我的樹http://www.wgraj.org/uploads/photos/simple/screen_6adc371c220e99f428fd221552609358.JPG – 2012-04-13 10:37:19

+0

你爲什麼要給我看你的項目樹?我在談論正確縮進代碼。由於SO不是付費支持網站,因此讓您的問題可讀性最低。 – 2012-04-13 10:42:34

回答

0

警告1個警告C4627: '#包括':尋找預編譯頭使用C時跳過:\ Documents和Settings ...

表示「預編譯頭文件」包括

#include "stdafx.h" 

應該是第一個包含的頭文件。

+0

作爲附錄,默認情況下,Visual Studio中的所有(非託管的)C++項目都使用預編譯頭文件。在這樣的項目中,_all_源文件_must_包含「stdafx.h」作爲第一個包含,或者獲得與OP相同的錯誤。 – 2012-04-13 11:14:17