c++链接mysql(vs2015)

第一次弄,不是很懂,把今天做的记录下来供以后查阅。


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冲突

更新:
因为基本都是在win32平台开发。所以去官网下载了win32(x86)版本可用的mysql,因为我本来安装了x64版本的,所以无需再次安装了,解压后找到lib和dll文件替换原来的即可。以下代码vs2015编译可通过。


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

#pragma comment(lib,"libmysql.lib")
#pragma comment(lib, "ws2_32.lib")
#include "stdafx.h"
#include <winsock.h>
#include "mysql.h"
#include <iostream>
#include <stdlib.h>


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;
}

> 代码参考:http://blog.csdn.net/ggz631047367/article/details/48157473
> 配置参考:
http://www.cnblogs.com/justinzhang/archive/2011/09/23/2185963.html

你可能感兴趣的:(c++)