//一次取一個值的情況,另一種情況與其類似,修改處會標出 #include #include #include #include using namespace std; void mysql_err_function(MYSQL * connection); void mysql_display(MYSQL * mysql,MYSQL_ROW sqlrow); 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 = "select * from tmp15"; //getline(cin,query); int res = mysql_query(connection,query.c_str()); if (res) { mysql_err_function(connection); } else { MYSQL_RES * my_res = mysql_use_result(connection); //將mysql_use_result改為mysql_store_result即可得到另一種情況的結果(其實是相同的...) if (my_res) { MYSQL_ROW sqlrow; while ((sqlrow = mysql_fetch_row(my_res))) { mysql_display(connection,sqlrow); } 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); } } void mysql_err_function(MYSQL * connection) { if (mysql_errno(connection)) { cout << "Error " << mysql_errno(connection) << " : " << mysql_error(connection) << endl; exit(-1); } } void mysql_display(MYSQL * mysql,MYSQL_ROW sqlrow) { for (unsigned int i = 0; i < mysql_field_count(mysql); ++i) { printf("%s ",sqlrow[i]); //cout << sqlrow[i] << ' '; //不知到為什麼將printf換成cout之後,打印值就會出錯...思考ing... } cout << endl; }
1、MYSQL_FIELD *mysql_fetch_field(MYSQL_RES *result); 2、MYSQL_FIELD定義: typedef struct st_mysql_field
{ char *name; /* Name of column */ char *table; /* Table of column if column was a field */ char *org_table; /* Org table name if table was an alias */ char *db; /* Database for table */ char *def; /* Default value (set by mysql_list_fields) */ unsigned long length; /* Width of column */ unsigned long max_length; /* Max width of selected set */ unsigned int flags; /* Div flags */ unsigned int decimals; /* Number of decimals in field */ enum enum_field_types type; /* Type of field. Se mysql_com.h for types */ } MYSQL_FIELD;
3、IS_NUM宏,若字段類型是數字形式的,則返回真。 if (IS_NUM(mysql_field_ptr -> type)) { cout << "Number" << endl; } 4、MYSQL_FIELD_OFFSET mysql_field_seek(MYSQL_RES * result, MYSQL_FIELD_OFFSET offset); //函數將字段光標設置到給定的偏移量offset,下一次調用mysql_fetch_field將檢索與該偏移量關聯的列的字段定義。如果鑰定位行的開始,則要傳遞一個值為0的offset值。
#include #include #include #include using namespace std; void mysql_err_function(MYSQL * connection); //實現參照上例 void mysql_display(MYSQL * mysql,MYSQL_ROW sqlrow); //實現參照上例 void mysql_display_head(MYSQL_RES * my_res); 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 = "select * from tmp15"; //getline(cin,query); int res = mysql_query(connection,query.c_str()); if (res) { mysql_err_function(connection); } else { MYSQL_RES * my_res = mysql_use_result(connection); if (my_res) { mysql_display_head(my_res); MYSQL_ROW sqlrow; cout << "Column Details:" << endl; while ((sqlrow = mysql_fetch_row(my_res))) { mysql_display(connection,sqlrow); } 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); } } void mysql_display_head(MYSQL_RES * my_res) { MYSQL_FIELD * my_field; cout << "Column Describe:" << endl; while ((my_field = mysql_fetch_field(my_res))) { cout << "\tName: " << my_field -> name << endl; cout << "\tType: "; if (IS_NUM(my_field -> type)) { cout << "Numeric field"; } else { switch(my_field -> type) { case FIELD_TYPE_VAR_STRING: cout << "Varchar"; break; case FIELD_TYPE_LONG: cout << "Long"; break; default: cout << "is " << my_field -> type << ",check in mysql.h"; break; } } cout << endl; cout << "\tMax width: " << my_field -> length << endl; if (my_field -> flags & AUTO_INCREMENT_FLAG) { cout << "\tAUTO_INCREMENT" << endl; } cout << endl; } }