本文为大家分享了Eclipse中C++连接mysql数据库的具体代码,供大家参考,具体内容如下
MinGW的配置在C连mysql里已经提到,这里只说C++跟C配置不同的地方。
首先觉得C++比C方便多了,不用生成.a连接文件,直接使用.lib文件即可
导入.h文件所在目录到C++ Compiler中的includes
添加lib文件搜索
代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
#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; } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/suwing6406029/article/details/39137723