#include #include #include #include using namespace std; void mysql_err_function(MYSQL * connection); int main() { MYSQL * connection; connection = mysql_init(NULL); if (!connection) { mysql_err_function(connection); } connection = mysql_real_connect(connection,"localhost","root","123456","test",0,NULL,0); if (!connection) { mysql_err_function(connection); } cout << "Connection to MySQL Server is Success..." << endl; string query; getline(cin,query); int res = mysql_query(connection,query.c_str()); if (res) { mysql_err_function(connection); } MYSQL_RES * my_res = mysql_store_result(connection); cout << "Retrieved " << mysql_num_rows(my_res) << "rows" << endl; MYSQL_ROW sqlrow; while ((sqlrow = mysql_fetch_row(my_res))) { cout << "Fetched data..." << endl; } mysql_free_result(my_res); mysql_close(connection); cout << "Connection to MySQL Server is closed!" << endl; return 0; } void mysql_err_function(MYSQL * connection) { if (mysql_errno(connection)) { cout << "Error " << mysql_errno(connection) << " : " << mysql_error(connection) << endl; exit(-1); } } [cpp] view plaincopyprint?在CODE上查看代碼片派生到我的代碼片 MYSQL_RES *mysql_use_result(MYSQL * connection); //成功返回結果集,失敗返回NULL [cpp] view plaincopyprint?在CODE上查看代碼片派生到我的代碼片 #include #include #include using namespace std; void mysql_err_function(MYSQL * connection); int main() { MYSQL * connection; connection = mysql_init(NULL); if (mysql_real_connect(connection,"localhost","root","123456","test",0,NULL,0)) { cout << "Connection to MySQL Server is Succeed..." << endl; string query; getline(cin,query); int res = mysql_query(connection,query.c_str()); if (res) { mysql_err_function(connection);//mysql_err_function()實現代碼參考上例 } else { MYSQL_RES * my_res = mysql_use_result(connection); if (my_res) { MYSQL_ROW sqlrow; while ((sqlrow = mysql_fetch_row(my_res))) { cout << "Fetching the Data..." << endl; } mysql_free_result(my_res); } else { mysql_err_function(connection); } } mysql_close(connection); cout << "Connection to MySQL Server is Closed!" << endl; } else { mysql_err_function(connection); } }