C/C++连接mysql以及CURD
来源:互联网 发布:php中使用url传递数组 编辑:程序博客网 时间:2024/05/17 14:28
步骤:
1. 安装MySQL数据库
2. 项目属性页->C/C++->常规->附加包含目录:xxx\mysql Server 5.6\include
3. 项目属性页->链接器->常规->附加库目录:xxx\MySQL Server 5.6\lib
4. 项目属性页->链接器->输入->附加依赖项:libmysql.lib 将libmysql.dll拷贝到项目中
5. 引入头文件:#include<Windows.h> #include <mysql.h>
测试代码:
#include<iostream>#include<Windows.h>#include <mysql.h>#include<string>using namespace std;int main(){ //必备的一个数据结构 MYSQL mydata; //初始化数据库 if (0 == mysql_library_init(0, NULL, NULL)) { cout << "mysql_library_init() succeed" << endl; } else { cout << "mysql_library_init() failed" << endl; return -1; } //初始化数据结构 if (NULL != mysql_init(&mydata)) { cout << "mysql_init() succeed" << endl; } else { cout << "mysql_init() failed" << endl; return -1; } //在连接数据库之前,设置额外的连接选项 //可以设置的选项很多,这里设置字符集,否则无法处理中文 if (0 == mysql_options(&mydata, MYSQL_SET_CHARSET_NAME, "gbk")) { cout << "mysql_options() succeed" << endl; } else { cout << "mysql_options() failed" << endl; return -1; } //连接数据库 if (NULL != mysql_real_connect(&mydata, "localhost", "root", "root", "test", 3306, NULL, 0)) //这里的地址,用户名,密码,端口可以根据自己本地的情况更改 { cout << "mysql_real_connect() succeed" << endl; } else { cout << "mysql_real_connect() failed" << endl; return -1; } //sql字符串 string sqlstr; //创建一个表 sqlstr = "CREATE TABLE IF NOT EXISTS `new_paper` ("; sqlstr += " `NewID` int(11) NOT NULL AUTO_INCREMENT,"; sqlstr += " `NewCaption` varchar(40) NOT NULL,"; sqlstr += " `NewContent` text,"; sqlstr += " `NewTime` datetime DEFAULT NULL,"; sqlstr += " PRIMARY KEY(`NewID`)"; sqlstr += " ) ENGINE = InnoDB DEFAULT CHARSET = utf8"; if (0 == mysql_query(&mydata, sqlstr.c_str())) { cout << "mysql_query() create table succeed" << endl; } else { cout << "mysql_query() create table failed" << endl; mysql_close(&mydata); return -1; } //向表中插入数据 for (int i = 0;i < 100;i++) { sqlstr = "INSERT INTO `test`.`new_paper` (`NewID`, `NewCaption`, `NewContent`, `NewTime`) "; sqlstr += "VALUES (default, '组织者', '方提出更换存放骨灰存放', '2015-09-01 14:29:51');"; if (0 == mysql_query(&mydata, sqlstr.c_str())) { cout << "mysql_query() insert data succeed" << endl; } else { cout << "mysql_query() insert data failed" << endl; mysql_close(&mydata); return -1; } } //显示刚才插入的数据 sqlstr = "SELECT `NewID`,`NewCaption`,`NewContent`,`NewTime` FROM `test`.`new_paper`"; 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 (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; } //删除刚才建的表 sqlstr = "DROP TABLE `test`.`new_paper`"; if (0 == mysql_query(&mydata, sqlstr.c_str())) { cout << "mysql_query() drop table succeed" << endl; } else { cout << "mysql_query() drop table failed" << endl; mysql_close(&mydata); return -1; } mysql_free_result(result); mysql_close(&mydata); mysql_server_end(); system("pause"); return 0;}
转自:http://blog.csdn.NET/ggz631047367/article/details/48157473
阅读全文
0 0
- C/C++连接mysql以及CURD
- CentOS 7 下的MySQL安装以及C连接MySQL
- C/C++如何连接MySQL服务器以及简单加密
- c连接MYSQL例子
- c连接mysql数据库
- C#ODBC连接MYSQL
- C 连接mysql
- VS2005 C 连接 MySql
- C语言连接mysql
- c连接mysql数据库
- C API连接MYSQL
- c 连接 mysql
- C语言连接Mysql
- C语言连接mysql
- c语言mysql连接
- C#--如何连接MySQL
- c语言连接MySQL
- c语言连接 mysql
- 步进电机的使用一
- Vector容器
- MOOC清华《面向对象程序设计》第4章:虚函数实验
- ubuntu14.04下部署smokeping监控软件
- centos7 部署全过程记录
- C/C++连接mysql以及CURD
- 2365 Rope
- Tomcat |Tomcat启动超时错误
- 创建scott的案例表代码
- acm学习
- mmm bionic avoid debug(fork)
- 算法系列——二叉搜索树和双向链表(剑指offer)
- 线段树——Luogu3870/BZOJ1230 [Usaco2008 Nov]lites 开关灯
- 微信小程序——获取用户unionId