Home >Database >Mysql Tutorial >C++操作mysql方法总结(1)

C++操作mysql方法总结(1)

WBOY
WBOYOriginal
2016-05-31 08:49:08876browse

列举出了C++操作mysql数据库的几种方法

通过mysql的c api和通过mysql的Connector C++ 1.1.3进行操作的两种方式

使用vs2013和64位的msql 5.6.16进行操作

image

项目中使用的数据库名为booktik

表为book

image

……….

(共有30条记录,只列出了部分记录,14-30未列出)

一、通过mysql的C api进行操作

1、新建一个空工程

2、将D:/Program Files/MySQL/MySQL Server 5.6/include添加到项目的包含目录中(根据具体路径而定)

image

3、将D:/Program Files/MySQL/MySQL Server 5.6/lib添加到项目的库目录中(根据具体路径而定)

image

4、添加libmysql.lib至附加依赖项中

image

( *3.4步 也可以在程序代码的开始处加上#pragma comment(lib,"D://Program Files//MySQL//MySQL Server 5.6//lib//libmysql.lib") 来导入libmysql.lib )

5、如果使用的mysql是64位的,还需要将项目的解决方案平台由win32改成x64

image

6、将D:/Program Files/MySQL/MySQL Server 5.6/lib(根据具体路径而定)下的libmysql.dll复制到项目中去,和.cpp,.h文件位于同一路径下

至此,相关配置全部完成

程序代码

main.cpp

#include <windows.h>#include <mysql.h>#include <string>#include <iostream>using namespace std;#pragma comment(lib,"D://Program Files//MySQL//MySQL Server 5.6//lib//libmysql.lib") int main(){		const char user[] = "root";		 	const char pswd[] = "123456";			const char host[] = "localhost";		const char table[] = "booktik";	 	unsigned int port = 3306;					MYSQL myCont;	MYSQL_RES *result;	MYSQL_ROW sql_row;	int res;	mysql_init(&myCont);	if (mysql_real_connect(&myCont, host, user, pswd, table, port, NULL, 0))	{		mysql_query(&myCont, "SET NAMES GBK"); //设置编码格式		res = mysql_query(&myCont, "select * from book");//查询		if (!res)		{			result = mysql_store_result(&myCont);			if (result)			{				while (sql_row = mysql_fetch_row(result))//获取具体的数据				{					cout<p>  <strong>运行结果如下:</strong>  </p>
<p> <img title="image" src="http://img.bitscn.com/upimg/allimg/c140719/1405K53D43K0-J0Q.jpg" alt="image"> </p>
<p>  <strong>二、通过mysql的Connector C++ 1.1.3进行操作</strong>  </p>
<p>MySQL C++ Driver的实现基于JDBC规范</p>
<p>MySQL Connector/C++是由Sun Microsystems开发的MySQL连接器。它提供了基于OO的编程接口与数据库驱动来操作MySQL服务器。</p>
<p> 与许多其他现存的C++接口实现不同,Connector/C++遵循了JDBC规范。也就是说,Connector/C++ Driver的API主要是基于Java语言的JDBC接口。JDBC是java语言与各种数据库连接的标准工业接口。 </p>
<p> Connector/C++实现了大部分JDBC规范。如果C++程序的开发者很熟悉JDBC编程,将很快的入门。 </p>
<p> MySQL Connector/C++需要安装配置boost库,boost库安装编译在这里不进行阐述 </p>
<p> <strong>1、新建一个空工程</strong> </p>
<p> <strong>2、将D:/Program Files/MySQL/Connector C++ 1.1.3/include添加到项目的包含目录中(视mysql安装路径而定)</strong> </p>
<p> <img title="image" src="http://img.bitscn.com/upimg/allimg/c140719/1405K53DU940-W4I.jpg" alt="image"> </p>
<p> <strong>3、将D:/boost/boost_1_55_0添加到项目的包含目录中(视mysql安装路径而定)</strong> </p>
<p> <img title="image" src="http://img.bitscn.com/upimg/allimg/c140719/1405K53E2Q20-91N9.jpg" alt="image"> </p>
<p> <strong>4、将D:/Program Files/MySQL/Connector C++ 1.1.3/lib/opt添加到项目的库目录中(根据具体路径而定)</strong> </p>
<p> <img title="image" src="http://img.bitscn.com/upimg/allimg/c140719/1405K53EI440-105144.jpg" alt="image"> </p>
<p> <strong>5、添加mysqlcppconn.lib至附加依赖项中</strong> </p>
<p> <img title="image" src="http://img.bitscn.com/upimg/allimg/c140719/1405K53F1W50-11E64.jpg" alt="image"> </p>
<p> <strong>6、如果使用的mysql是64位的,还需要将项目的解决方案平台由win32改成x64</strong> </p>
<p> <img title="image" src="http://img.bitscn.com/upimg/allimg/c140719/1405K53FC1Z-122958.jpg" alt="image"> </p>
<p> 7、将D:/Program Files/MySQL/Connector C++ 1.1.3/lib/opt(根据具体路径而定)下的mysqlcppconn.dll复制到项目中去,和.cpp,.h文件位于同一路径下 </p>
<p> 将D:/Program Files/MySQL/MySQL Server 5.6/lib(根据具体路径而定)下的libmysql.dll复制到项目中去,和.cpp,.h文件位于同一路径下 </p>
<p>至此,相关配置全部完成</p>
<p> <strong> 程序代码 </strong> </p>
<p> main.cpp </p>
<pre class="brush:php;toolbar:false">#include <iostream>#include <map>#include <string>#include <memory>#include "mysql_driver.h"#include "mysql_connection.h"#include "cppconn/driver.h"#include "cppconn/statement.h"#include "cppconn/prepared_statement.h"#include "cppconn/metadata.h"#include "cppconn/exception.h"using namespace std;using namespace sql;int main(){	sql::mysql::MySQL_Driver *driver = 0;	sql::Connection *conn = 0;	try	{		driver = sql::mysql::get_mysql_driver_instance();		conn = driver->connect("tcp://localhost:3306/booktik", "root", "123456");		cout createStatement();	stat->execute("set names 'gbk'");	ResultSet *res;	res = stat->executeQuery("SELECT * FROM BOOK");	while (res->next())	{		cout getString("bookname") getString("size") <p>  <strong>运行结果</strong>  </p>
<p> <img title="image" src="http://img.bitscn.com/upimg/allimg/c140719/1405K53G1250-135491.jpg" alt="image"> </p>
    </memory></string></map></iostream>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn