2016-03-23 23 views
4

私は助けが必要です。私はこのコードを(下記)、MySQLテーブルにデータを追加して、同じテーブルを返すようにしました。テーブルからMySQLデータを表示するC++のexecuteQuery()エラー

SQL error. Error message: 

文字通り空白:コードは、私はそれを実行したとき、それはMySQLのテーブルに列を追加しますが、それは誤りで、停止し、罰金やっています。 INCLUDEexecuteQuery()にあるものの、SELECTステートメントを使用すると、問題なく実行され、エラーメッセージが表示されず、テーブル(またはその一部)が表示されます。私は何が欠けていますか?

私はVisual Studios 2015とMySQL Serverを使用しています。

最終目標は、C++を使用してAPIをSQLテーブルに接続して、特定のタイムパンドに従ってデータを記録することです。これは最初のステップの1つです。ちょうど私がMySQLとC++を適切にリンクできることを確認することです。

申し訳ありませんが、この記事はあまり書かれていないのですが、初心者では経験豊富なプログラマーではありません...また、他のスレッドも見ましたが、事前に

// Standard 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" 

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

// Specify our connection target and credentials 
const string server = "127.0.0.1:3306"; 
const string username = "root"; 
const string password = "root"; 
const string database = "dbex"; 
const string table = "tbex"; 

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); 
     dbConn->setSchema(database); 
    } 
    catch (sql::SQLException e) 
    { 
     cout << "Could not connect to database. Error message: " << e.what() << endl; 
     system("pause"); 
     exit(1); 
    } 
    stmt = dbConn->createStatement(); 

    // Try to query the database 
    try 
    { 
     //stmt->execute("USE " + database);    // Select which database to use. Notice that we use "execute" to perform a command. 
     res = stmt->executeQuery("INSERT INTO "+ table +"(Brand, Model, Power, `Last Used`,`# Times Used`) VALUES('Ferrari','Modena','500','Never',0)"); 
     res = stmt->executeQuery("SELECT * FROM cars"); // 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()) 
    { 
     cout << res->getString(1) << endl; 
    } 

    delete res; 
    delete stmt; 
    delete dbConn; 

    system("pause"); 
    return 0; 
} 

おかげでこの

答えて

0

チェックイン:行の

res = stmt->executeQuery("INSERT INTO "+ table +"(Brand, Model, Power, `Last Used`,`# Times Used`) VALUES('Ferrari','Modena','500','Never',0)"); 

あなたは+(プラス)オペレータがいることを動作しないこと、間違った文字列の連結を作っていますそのコードは文字列を連結しないで、代わりにポインタを追加します。

ただ単にこの方法を交換して、再試行してください:

#define TABLE "tbex"// put this in top of cpp file 
...... 
res = stmt->executeQuery("INSERT INTO " TABLE "(Brand, Model, Power, `Last Used` 
,`# Times Used`) VALUES('Ferrari','Modena','500','Never',0)"); 
+0

返信いただきありがとうございます。私はこれを試し、前と同じ問題があった:行がテーブルに追加されますが、空のエラーメッセージを表示し、追加後にコードを停止します – tomasvc

0

INSERTはクエリではありません。 executeQuery()の代わりにexecuteUpdate()を使用してみてください。概念の一例のための公式のMySQLの例here

sql::PreparedStatement *pstmt; 

pstmt = con->prepareStatement("INSERT INTO "+ table +"(Brand, Model, Power, `Last Used`,`# Times Used`) VALUES('Ferrari','Modena','500','Never',0)"); 
res = pstmt->executeUpdate(); 
delete pstmt; 

はルック:

は、この行に次の行(追加の.hファイルが必要な場合があります)と

res = stmt->executeQuery("INSERT INTO "+ table +"(Brand, Model, Power, `Last Used`,`# Times Used`) VALUES('Ferrari','Modena','500','Never',0)"); 

を交換してください。

this Stackoverflow質問に示すように、execute()を試してみることもできます。関数execute()は一般的なSQLコマンドに使用されますが、より多くの指定された関数(戻り値はブーリアン)として戻り値の冗長性を保つことはできません。

関連する問題