怎么在Eclipse中使用C++连接mysql数据库
今天就跟大家聊聊有关怎么在Eclipse中使用C++连接mysql数据库,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。
首先觉得C++比C方便多了,不用生成.a连接文件,直接使用.lib文件即可
导入.h文件所在目录到C++ Compiler中的includes
添加lib文件搜索
代码:
#include <winsock.h> #include <iostream> #include <string> #include <mysql.h> using namespace std; int main() { MYSQL mydata; //初始化数据库 mysql_library_init(0, NULL, NULL); mysql_init(&mydata); mysql_options(&mydata, MYSQL_SET_CHARSET_NAME, "gbk"); //连接数据库 if (NULL != mysql_real_connect(&mydata, "localhost", "root", "111111", "mysql", 3306, NULL, 0)) { cout << "mysql_real_connect() succeed" << endl; } else { cout << "mysql_real_connect() failed" << endl; return -1; } string sqlstr = "SELECT * FROM ME_MENU"; MYSQL_RES *result = NULL; if (0 == mysql_query(&mydata, sqlstr.c_str())) { cout << "mysql_query() select data succeed" << endl; result = mysql_store_result(&mydata); int rowcount = mysql_num_rows(result); cout << "row count: " << rowcount << endl; unsigned int fieldcount = mysql_num_fields(result); MYSQL_FIELD *field = NULL; for (unsigned int i = 0; i < fieldcount; i++) { field = mysql_fetch_field_direct(result, i); cout << field->name << "\t\t"; } cout << endl; MYSQL_ROW row = NULL; row = mysql_fetch_row(result); while (NULL != row) { for (unsigned int i = 0; i < fieldcount; i++) { cout << row[i] << "\t\t"; } cout << endl; row = mysql_fetch_row(result); } } else { cout << "mysql_query() select data failed" << endl; mysql_close(&mydata); return -1; } mysql_free_result(result); mysql_close(&mydata); mysql_server_end(); system("pause"); return 0; }
看完上述内容,你们对怎么在Eclipse中使用C++连接mysql数据库有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注行业资讯频道,感谢大家的支持。