Fork me on GitHub

C++操作mySql数据库

1.首先下载配置mySql(可参考:http://jingyan.baidu.com/article/f3ad7d0ffc061a09c3345bf0.html)

2.配置VC环境

(1)将libmysql.lib文件拷贝到安装路径VC/bin下

(2)将libmysql.dll文件拷贝到windows/system32下(没有这一步,运行提示会报“无法找到libmysql.dll”的错误)

(3)配置属性C/C++ —> 常规 —> 附件包含目录添加D:\mySQL\mysql_5.6.24_winx64\lib

(4)配置属性链接器 —> 常规 —> 附件包含目录添加D:\mySQL\mysql_5.6.24_winx64\lib

(5)配置属性链接器 —> 输入 —> 附加依赖性添加libmysql.lib

3.写代码

代码示例:

  1 #include <winsock.h>
  2 #include <iostream>
  3 #include <string>
  4 #include <mysql.h>
  5 using namespace std;
  6 
  7 int mainconnectMysql()
  8 {
  9     //必备的一个数据结构
 10     MYSQL mydata;
 11 
 12     //初始化数据库
 13     if (0 == mysql_library_init(0, NULL, NULL))
 14     {
 15         cout << "mysql_library_init() succeed" << endl;
 16     }
 17     else
 18     {
 19         cout << "mysql_library_init() failed" << endl;
 20         return -1;
 21     }
 22 
 23     //初始化数据结构
 24     if (NULL != mysql_init(&mydata))
 25     {
 26         cout << "mysql_init() succeed" << endl;
 27     }
 28     else
 29     {
 30         cout << "mysql_init() failed" << endl;
 31         return -1;
 32     }
 33 
 34     //在连接数据库之前,设置额外的连接选项
 35     //可以设置的选项很多,这里设置字符集,否则无法处理中文
 36 //    if (0 == mysql_options(&mydata, MYSQL_SET_CHARSET_NAME, "gbk"))
 37     if (0 == mysql_options(&mydata, MYSQL_SET_CHARSET_NAME, "gbk"))
 38     {
 39         cout << "mysql_options() succeed" << endl;
 40     }
 41     else
 42     {
 43         cout << "mysql_options() failed" << endl;
 44         return -1;
 45     }
 46 
 47     //连接数据库
 48     if (NULL != mysql_real_connect(&mydata, "127.0.0.1", "root", "110", "test", 3306, NULL, 0))  //"127.0.0.1" <=====> "localhost"
 49         //这里的地址,用户名,密码,端口可以根据自己本地的情况更改
 50     {
 51         cout << "mysql_real_connect() succeed" << endl;
 52     }
 53     else
 54     {
 55         cout << "mysql_real_connect() failed" << endl;
 56         return -1;
 57     }
 58 
 59 
 60 
 61     //sql字符串
 62     string sqlstr;
 63 
 64     //创建一个表
 65 //    sqlstr = "CREATE TABLE IF NOT EXISTS Heros";
 66 //    sqlstr += "(ID char(1) not null,NAME varchar(10) not null,SEX char(1) not null,AGE int(2) not null);";
 67 
 68     //====================
 69         sqlstr = "CREATE TABLE IF NOT EXISTS Heros";
 70         sqlstr += "(";
 71         sqlstr +="id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT 'Unique User ID',";
 72         sqlstr +="name VARCHAR(100) CHARACTER SET gb2312 COLLATE gb2312_chinese_ci NULL COMMENT 'Name Of User',";  //gb2312_chinese_CI : 只支持简体中文
 73         sqlstr += "sex Char(1) NOT NULL,";
 74         sqlstr +="age INT UNSIGNED NOT NULL DEFAULT 0 COMMENT 'The Summation Of Using Time'";
 75         sqlstr += ");";
 76 
 77     if (0 == mysql_query(&mydata, sqlstr.c_str()))
 78     {
 79         cout << "mysql_query() create table succeed" << endl;
 80     }
 81     else
 82     {
 83         cout << "mysql_query() create table failed" << endl;
 84         mysql_close(&mydata);
 85         return -1;
 86     }
 87 
 88     //===========================
 89     //mysql_query(&mydata, "SET NAMES latin1");
 90     //===========================
 91 
 92     //向表中插入数据
 93 //    sqlstr = "INSERT INTO Heros(ID,NAME,SEX,AGE) VALUES('1','宋江','m',45),('2','卢俊义','f',40), ('3', '吴用', 'm', 39),('4','公孙胜','m',36),('5', '关胜', 'm', 38), ('6', '林冲', 'f', 34);";
 94     sqlstr = "INSERT INTO Heros(NAME,SEX,AGE) VALUES('宋江','m',45),('卢俊义','f',40), ('吴用', 'm', 39),('公孙胜','m',36),('关胜', 'm', 38), ('林冲', 'f', 34);";
 95 //    sqlstr = "INSERT INTO Heros(NAME,SEX) VALUES('宋江','m'),('卢俊义','f'), ('吴用', 'm'),('公孙胜','m'),('关胜', 'm'), ('林冲', 'f');";
 96 
 97     
 98     if (0 == mysql_query(&mydata, sqlstr.c_str()))
 99     {
100         cout << "mysql_query() insert data succeed" << endl;
101     }
102     else
103     {
104         cout << "mysql_query() insert data failed" << endl;
105         mysql_close(&mydata);
106         return -1;
107     }
108 
109     //显示刚才插入的数据
110     sqlstr = "SELECT id,name,sex,age FROM Heros";
111     MYSQL_RES *result = NULL;
112     if (0 == mysql_query(&mydata, sqlstr.c_str()))
113     {
114         cout << "mysql_query() select data succeed" << endl;
115 
116         //一次性取得数据集
117         result = mysql_store_result(&mydata);
118         //取得并打印行数
119         int rowcount = mysql_num_rows(result);
120         cout << "row count: " << rowcount << endl;
121 
122         //取得并打印各字段的名称
123         unsigned int fieldcount = mysql_num_fields(result);
124         MYSQL_FIELD *field = NULL;
125         for (unsigned int i = 0; i < fieldcount; i++)
126         {
127             field = mysql_fetch_field_direct(result, i);
128             cout << field->name << "\t\t";
129         }
130         cout << endl;
131 
132         //打印各行
133         MYSQL_ROW row = NULL;
134         row = mysql_fetch_row(result);
135         while (NULL != row)
136         {
137             for (int i = 0; i < fieldcount; i++)
138             {
139                 cout << row[i] << "\t\t";
140             }
141             cout << endl;
142             row = mysql_fetch_row(result);
143         }
144 
145     }
146     else
147     {
148         cout << "mysql_query() select data failed" << endl;
149         mysql_close(&mydata);
150         return -1;
151     }
152 
153 //    //删除刚才建的表
154 //    sqlstr = "DROP TABLE user_info";
155 //    if (0 == mysql_query(&mydata, sqlstr.c_str()))
156 //    {
157 //        cout << "mysql_query() drop table succeed" << endl;
158 //    }
159 //    else
160 //    {
161 //        cout << "mysql_query() drop table failed" << endl;
162 //        mysql_close(&mydata);
163 //        return -1;
164 //    }
165 //    mysql_free_result(result);
166 //    mysql_close(&mydata);
167 //    mysql_server_end();
168 
169     system("pause");
170     return 0;
171 }

 

posted @ 2016-06-18 09:48  千秋此意  阅读(408)  评论(0编辑  收藏  举报