一个实现C/C++连接MYSQL数据库的很好的例子
标签: <无>
代码片段(1)
[代码] [C/C++]代码
001 | //下面的代码是一个实现C++连接MYSQL数据库的很好的例子 |
002 | //这里用了建表,插入,检索,删表等常用功能 |
003 | //我用VC++6.0生成,已经成功连接了。 |
004 | //在VC++6.0中要想把做一下两步准备工作才可以。 |
005 | //(1)Tools->Options->Directories->Include files中添加C:\Program Files\MySQL\MySQL Server 6.0\include |
006 | //(2)Tools->Options->Directories->Library files中添加C:\Program Files\MySQL\MySQL Server 6.0\lib\opt |
008 | //我觉得这个例子写的很好,再结合自己的试验,特地介绍给大家! |
016 | #pragma comment(lib,"ws2_32.lib") |
017 | #pragma comment(lib,"libmysql.lib") |
023 | cout <<
"****************************************" << endl; |
033 | if
(0 == mysql_library_init(0,NULL,NULL)) { |
034 | cout << "mysql_library_init() succeed" << endl; |
036 | cout << "mysql_library_init() failed" << endl; |
045 | if
(NULL != mysql_init(&mydata)) { |
046 | cout << "mysql_init() succeed" << endl; |
048 | cout << "mysql_init() failed" << endl; |
057 | //可以设置的选项很多,这里设置字符集,否则无法处理中文 |
058 | if
(0 == mysql_options(&mydata,MYSQL_SET_CHARSET_NAME, "gbk" )) { |
059 | cout << "mysql_options() succeed" << endl; |
061 | cout << "mysql_options() failed" << endl; |
071 | != mysql_real_connect(&mydata, "localhost" , "root" , "test" , "test" , |
073 | //这里的地址,用户名,密码,端口可以根据自己本地的情况更改 |
075 | cout << "mysql_real_connect() succeed" << endl; |
077 | cout << "mysql_real_connect() failed" << endl; |
089 | sqlstr =
"CREATE TABLE IF NOT EXISTS user_info" ; |
092 | "user_id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT 'Unique User ID'," ; |
094 | "user_name VARCHAR(100) CHARACTER SET gb2312 COLLATE gb2312_chinese_ci NULL COMMENT 'Name Of User'," ; |
096 | "user_second_sum INT UNSIGNED NOT NULL DEFAULT 0 COMMENT 'The Summation Of Using Time'" ; |
098 | if
(0 == mysql_query(&mydata,sqlstr.c_str())) { |
099 | cout << "mysql_query() create table succeed" << endl; |
101 | cout << "mysql_query() create table failed" << endl; |
112 | "INSERT INTO user_info(user_name) VALUES('公司名称'),('一级部门'),('二级部门'),('开发小组'),('姓名');" ; |
113 | if
(0 == mysql_query(&mydata,sqlstr.c_str())) { |
114 | cout << "mysql_query() insert data succeed" << endl; |
116 | cout << "mysql_query() insert data failed" << endl; |
126 | sqlstr =
"SELECT user_id,user_name,user_second_sum FROM user_info" ; |
127 | MYSQL_RES *result = NULL; |
128 | if
(0 == mysql_query(&mydata,sqlstr.c_str())) { |
129 | cout << "mysql_query() select data succeed" << endl; |
132 | result = mysql_store_result(&mydata); |
134 | int
rowcount = mysql_num_rows(result); |
135 | cout << "row count: " << rowcount << endl; |
138 | unsigned int fieldcount = mysql_num_fields(result); |
139 | MYSQL_FIELD *field = NULL; |
140 | for
(unsigned int i = 0; i < fieldcount; i++) { |
141 | field = mysql_fetch_field_direct(result,i); |
142 | cout <<field->name <<
"\t\t" ; |
148 | row = mysql_fetch_row(result); |
150 | for
( int i = 0; i < fieldcount; i++) { |
151 | cout <<row[i] <<
"\t\t" ; |
154 | row = mysql_fetch_row(result); |
158 | cout << "mysql_query() select data failed" << endl; |
168 | sqlstr =
"DROP TABLE user_info" ; |
169 | if
(0 == mysql_query(&mydata,sqlstr.c_str())) { |
170 | cout << "mysql_query() drop table succeed" << endl; |
172 | cout << "mysql_query() drop table failed" << endl; |
176 | mysql_free_result(result); |
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理