c++链接mysql
1.建立一个控制台项目。
2.指定包含头文件路径:
Project->properties->C/C++->Additional Include Directories->(这里指定mysql各种头文件的目录,我的是mysql文件夹里的include)
3.指定lib路径:
Project->properties->Linker->General->Additional Library Directories->(这里是libmysql.lib所在路径,我的是在lib里)
4.添加lib:
Project->properties->Linker->Input->Additional Dependencies->增加libmysql.lib
5.修改配置:
Build->Configuration Manager->Active solution platform->x64
(为了避免模块计算机类型x64与目标计算机类型x86冲突

这里只用一个main.cpp做了简单的demo:

#pragma comment(lib,"libmysql.lib")
#pragma comment(lib, "ws2_32.lib")
#include "stdafx.h"
#include
#include "mysql.h"
#include
#include
int main()
{
MYSQL mydata;
//初始化
if (0 == mysql_library_init(0,NULL,NULL)){
std::cout << "mysql_library_init() succeed" << std::endl;
}
else {
std::cout << "mysql_library_init() failed" << std::endl;
}
if (NULL != mysql_init(&mydata)){
std::cout << "mysql_init(mydata) succeed" << std::endl;
}
else {
std::cout << "mysql_init(mydata) failed" << std::endl;
return -1;
}
//处理中文
if (0 == mysql_options(&mydata, MYSQL_SET_CHARSET_NAME, "gbk")) {
std::cout << "mysql_options() succeed" << std::endl;
}
else {
std::cout << "mysql_options() failed" << std::endl;
}
//连接数据库
if (NULL!=mysql_real_connect(&mydata,"localhost","","","test",3306,NULL,0)) {
std::cout << "mysql_real_connect() succeed" << std::endl;
}
else {
std::cout << "mysql_real_connect() failed" << std::endl;
return -1;
}
std::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())) {
std::cout << "mysql_query() create table succeed" << std::endl;
}
else {
std::cout << "mysql_query() create table failed" << std::endl;
mysql_close(&mydata);
return -1;
}
//插入数据
for (int i = 0; i < 5; i++){
sqlstr = "INSERT INTO test.new_paper(NewID,NewCaption,NewContent,NewTime)";
sqlstr += "VALUES(default,'小明','吃了两个西瓜','2017-01-11');";
if (0 == mysql_query(&mydata, sqlstr.c_str())) {std::cout << "mysql_query() insert data succeed" << std::endl;
}
else {std::cout << "mysql_query() insert data failed" << std::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())) {
std::cout << "mysql_query() select data succeed" << std::endl;
result = mysql_store_result(&mydata);
int rowcount = mysql_num_rows(result);
std::cout << "row count:" << rowcount << std::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);std::cout << field->name << "\t\t";
}
std::cout << std::endl;
MYSQL_ROW row = NULL;
row = mysql_fetch_row(result);
while(NULL!=row) {for (int i = 0; i < fieldcount;i++) { std::cout << row[i] << "\t\t";}std::cout << std::endl;row = mysql_fetch_row(result);
}
}
else {
std::cout << "mysql_query() select data failed" << std::endl;
mysql_close(&mydata);
return -1;
}
//删除表
sqlstr = "DROP TABLE test.new_paper";
if (0 == mysql_query(&mydata, sqlstr.c_str())) {
std::cout << "mysql_query() drop table succeed" << std::endl;
}
else {
std::cout << "mysql_query() drop table failed" << std::endl;
mysql_close(&mydata);
return -1;
}
mysql_free_result(result);
mysql_close(&mydata);
mysql_server_end();
/*我做的时候是一块一块编译的,怕哪里出错,后面的暂停和输入都是为了看错误*/
system("pause");
char k;
std::cin >> k;
return 0;
}
.h>.h>

本文数据库(mysql)相关术语:navicat for mysql mysql workbench mysql数据库 mysql 存储过程 mysql安装图解 mysql教程 mysql 管理工具

主题: SQLMySQL数据C++InnoDBAU数据库AUTUTRY
分页:12
转载请注明
本文标题:c++链接mysql(vs2015) mysql mysql数据库 mysql函数 MySQL连接问题
本站链接:http://www.codesec.net/view/523173.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 数据库(mysql) | 评论(0) | 阅读(43)