1
我修改了一組來自MySQL頁面的代碼,將輸入數據測試到MySQL表'list'中。該程序退出時出現以下錯誤C++將數據插入到MySQL返回錯誤
terminate called after throwing an instance of 'sql::SQLException'
what():
Aborted
我檢查完表後,以某種方式成功插入數據。 我修改了這個程序,重複使用for循環插入數據,但最終迭代後退出循環(程序在第一次插入時強制退出)。
我也試過用硬編碼的查詢,它在MySQL中手動輸入,但仍然有同樣的問題。
#include <stdlib.h>
#include <iostream>
#include <string>
#include "mysql_connection.h"
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <mysql_driver.h>
using namespace std;
int main(void){
cout << endl;
cout << "Running test insert" << endl;
sql::Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
sql::ResultSet *res;
/* Create a connection */
driver = get_driver_instance();
con = driver->connect("localhost", "dev", "1234");
/* Connect to the MySQL test database */
con->setSchema("million");
string data("abc1234567890");
stmt = con->createStatement();
res = stmt->executeQuery("INSERT INTO list VALUES('0','" +
data + "','" + data +"','" + data + "','" + data + "','" + data + "');");
}
// replace with your statement
delete res;
delete stmt;
delete con;
cout << endl;
return EXIT_SUCCESS;
}
有了這個嘗試{}趕上
try{....}
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;}
輸出此錯誤消息
# ERR: SQLException in test.cpp(main) on line 54
# ERR: (MySQL error code: 0, SQLState: 00000)
** WARNING **:不要忘記正確逃生你的價值觀。你在這裏得到的是一個嚴重的SQL注入漏洞。將數據添加到查詢時,請務必使用[預準備語句](http://dev.mysql.com/doc/connector-cpp/en/connector-cpp-examples-prepared-statements.html)。 – tadman 2015-03-03 15:35:53
我建議在放入函數之前將您的SQL查詢格式化爲字符串。這將允許您使用調試器並在將字符串發送到MySQL數據庫之前檢查該字符串的內容。 – 2015-03-03 16:58:13
我得到了同樣的問題,任何更新?謝謝。 – 2016-01-26 02:39:44