2012-05-05 136 views
0

我正在開發一個連接到mySQL數據庫所需的Visual C++ 2010中的C++控制檯應用程序。我正在使用wamp服務器來連接mysql和mySQL C++連接器。代碼工作正常閱讀數據庫,但當我嘗試插入數據,它會給出意想不到的錯誤。任何人都有這樣的經歷?將C++程序連接到mysql時出現意外錯誤

這裏是我的全碼: (代碼的輸出是:「SQL錯誤:錯誤消息:未知異常」)

// Standad C++ includes 
#include <iostream> 
#include <cstdlib> 
#include <string> 
using namespace std; 

// Include the Connector/C++ headers 
#include "cppconn/driver.h" 
#include "cppconn/exception.h" 
#include "cppconn/resultset.h" 
#include "cppconn/statement.h" 
#include "cppconn/sqlstring.h" 

// Link to the Connector/C++ library 
#pragma comment(lib, "mysqlcppconn.lib") 

// Specify our connection target and credentials 
const string server = "tcp://127.0.0.1:3306"; 
const string username = "cashif"; 
const string password = "111222"; // No password - NEVER DO THIS ON A PRODUCTION SERVER! 

int main() 
{ 
    sql::Driver  *driver; // Create a pointer to a MySQL driver object 
    sql::Connection *dbConn; // Create a pointer to a database connection object 
    sql::Statement *stmt; // Create a pointer to a Statement object to hold our SQL commands 
    sql::ResultSet *res; // Create a pointer to a ResultSet object to hold the results of any queries we run 

    // Try to get a driver to use to connect to our DBMS 
    try 
    { 
     driver = get_driver_instance(); 
    } 
    catch (sql::SQLException e) 
    { 
     cout << "Could not get a database driver. Error message: " << e.what() << endl; 
     system("pause"); 
     exit(1); 
    } 

    // Try to connect to the DBMS server 
    try 
    { 
     dbConn = driver->connect(server, username, password); 
    } 
    catch (sql::SQLException e) 
    { 
     cout << "Could not connect to database. Error message: " << e.what() << endl; 
     system("pause"); 
     exit(1); 
    } 

    stmt = dbConn->createStatement(); // Specify which connection our SQL statement should be executed on 

    // Try to query the database 
    try 
    { 
     stmt->execute("use dengue_test");    // Select which database to use. Notice that we use "execute" to perform a command. 

     stmt->execute("insert into patients values(3,\"Amina\",\"Iqbal Town\""); // Perform a query and get the results. Notice that we use "executeQuery" to get results back 
    } 
    catch (sql::SQLException e) 
    { 
     cout << "SQL error. Error message: " << e.what() << endl; 
     system("pause"); 
     exit(1); 
    } 

    // While there are still results (i.e. rows/records) in our result set... 
/* 
    while (res->next()) 
    { 
     // ...get each field we want and output it to the screen 
     // Note: The first field/column in our result-set is field 1 (one) and -NOT- field 0 (zero) 
     // Also, if we know the name of the field then we can also get it directly by name by using: 
     // res->getString("TheNameOfTheField"); 
     cout << res->getString(1) << " - " << res->getString(2) << " - " << res->getString(3) << endl;     
    } 
    */ 
    // Clean up after ourselves 
    //delete res; 
    delete stmt; 
    delete dbConn; 

    system("pause"); 
    return 0; 
} 
+0

你可以發表病人的結構嗎? – Raam

回答

1

您的SQL兩個錯誤,一個MySQL將讓你逃避,一個不會。你也有一個壞習慣。

兩個錯誤就在這裏:

stmt->execute("insert into patients values(3,\"Amina\",\"Iqbal Town\""); 

在SQL字符串常量使用單引號,不是雙引號; MySQL會讓你逃脫這個。但是,你也有一個缺少的右括號,MySQL不喜歡那一點。你應該這樣說:

stmt->execute("insert into patients values(3, 'Amina', 'Iqbal Town')"); 

不指定在一個SQL INSERT列是一個壞習慣,所以你應該這樣說:

insert into patients (col1, col2, col3) values (...) 

其中col1和朋友,當然,真正的列名。表中的列沒有任何明確定義的順序,所以您應該具體說明以避免有趣的錯誤和維護噩夢。

+0

謝謝。這些都是非常愚蠢的錯誤。它的工作現在好了。還有一件事,它在發佈模式下不起作用。那是什麼問題? –

+0

@CashifIlyas:我不知道VS足夠好,知道爲什麼在發行模式中會出現問題,對不起。 –

相關問題