使用MySQL connector/C++链接MySQL数据库
2011-06-05 00:08
477 查看
首先去MySQL官网下载MySQL connector/C++:http://dev.mysql.com/downloads/connector/cpp/1.0.html
下载第二个包,windows32位非安装版(个人觉得这个包干净)。目前的版本是Connector/C++ 1.0.5。
Windows (x86, 32-bit), ZIP Archive (mysql-connector-c++-noinstall-1.0.5-win32.zip)
将整个包解压到项目文件夹下的的源文件目录。文件夹名字太长,将“mysql-connector-c++-noinstall-1.0.5-win32”改为“mysql”。
下面要配置vs2008的环境。
1. 项目属性页->C/C++->General->Additional Include Directories。将mysql/include目录和mysql/include/cppconn目录添加进去。
2. 项目属性页->Linker->General->Additional Library Directories。将mysql/lib目录添加进去。
3. 项目属性页->Linker->Input->Additional Dependencies。添加这两项mysqlcppconn.lib,mysqlcppconn-static.lib(mysql/lib目录下的两个.lib文件)
4. 将mysql/lib下的mysqlcppconn.dll文件复制到windows/system32文件夹下。
环境配置完毕。
在连接数据库之前,先建立一张表。 (其实这些可以在代码中完成,我这样是为了让测试代码尽可能简练易查错)
打开控制台,输入mysql -u root -p,输入密码。
查看当前已有的数据库。(SQL语句末尾加上';'表示立即执行当前语句。)
mysql> show databases;
创建数据库
mysql> create database test;
使用数据库(这句不能加分号)
mysql> use test
查看已有的表
mysql> show tables;
创建表
mysql> create table testuser ( id INT, name CHAR(20));
插入数据
mysql> insert into testuser(id, name) values(1001, 'google');
mysql> insert into testuser(id, name) values(1002, 'kingsoft');
mysql> insert into testuser(id, name) values(1003, 'firefox');
现在在C++中查询这些数据
#include "stdafx.h"
#include <mysql_connection.h>
#include <mysql_driver.h>
#include <statement.h>
using namespace sql;
using namespace std;
void RunConnectMySQL()
{
mysql::MySQL_Driver *driver;
Connection *con;
Statement *state;
ResultSet *result;
// 初始化驱动
driver = sql::mysql::get_mysql_driver_instance();
// 建立链接
con = driver->connect("tcp://127.0.0.1:3306", "root", "123");
state = con->createStatement();
state->execute("use test");
// 查询
result = state->executeQuery("select * from testuser where id < 1002");
// 输出查询
while(result->next())
{
int id = result->getInt("ID");
string name = result->getString("name");
cout << id << " : " << name << endl;
}
delete state;
delete con;
}
int _tmain(int argc, _TCHAR* argv[])
{
RunConnectMySQL();
getchar();
return 0;
}
以上是转载的网上的例子
///////////////////////////////////////////////////////////////////////
但在实际的操作过程中,却遇到了新的问题。
当我按照上面的步骤,依次完成Demo版本的检验,但在Debug版本下,却无法使用(VS2008)。
3. 项目属性页->Linker->Input->Additional Dependencies。添加这两项mysqlcppconn.lib,mysqlcppconn-static.lib(mysql/lib目录下的两个.lib文件)
当添加完这两个lib文件后,进行Debug调色运行,当运行到string name = result-getString("name");时出现错误。
是什么原因导致的呢!因为mysqlcppconn.lib或mysqlcppconn-static.lib是Release版本下的,不能做Debug版本的程序。string或vertor在释放内存时会报错。可能在编译时的依赖顺序不一样。
可在项目属性页->Linker->Input->Additional Dependencies。中添加msvcrt.lib msvcprt.lib 两个lib文件。使其支持Debug的调试。
下载第二个包,windows32位非安装版(个人觉得这个包干净)。目前的版本是Connector/C++ 1.0.5。
Windows (x86, 32-bit), ZIP Archive (mysql-connector-c++-noinstall-1.0.5-win32.zip)
将整个包解压到项目文件夹下的的源文件目录。文件夹名字太长,将“mysql-connector-c++-noinstall-1.0.5-win32”改为“mysql”。
下面要配置vs2008的环境。
1. 项目属性页->C/C++->General->Additional Include Directories。将mysql/include目录和mysql/include/cppconn目录添加进去。
2. 项目属性页->Linker->General->Additional Library Directories。将mysql/lib目录添加进去。
3. 项目属性页->Linker->Input->Additional Dependencies。添加这两项mysqlcppconn.lib,mysqlcppconn-static.lib(mysql/lib目录下的两个.lib文件)
4. 将mysql/lib下的mysqlcppconn.dll文件复制到windows/system32文件夹下。
环境配置完毕。
在连接数据库之前,先建立一张表。 (其实这些可以在代码中完成,我这样是为了让测试代码尽可能简练易查错)
打开控制台,输入mysql -u root -p,输入密码。
查看当前已有的数据库。(SQL语句末尾加上';'表示立即执行当前语句。)
mysql> show databases;
创建数据库
mysql> create database test;
使用数据库(这句不能加分号)
mysql> use test
查看已有的表
mysql> show tables;
创建表
mysql> create table testuser ( id INT, name CHAR(20));
插入数据
mysql> insert into testuser(id, name) values(1001, 'google');
mysql> insert into testuser(id, name) values(1002, 'kingsoft');
mysql> insert into testuser(id, name) values(1003, 'firefox');
现在在C++中查询这些数据
#include "stdafx.h"
#include <mysql_connection.h>
#include <mysql_driver.h>
#include <statement.h>
using namespace sql;
using namespace std;
void RunConnectMySQL()
{
mysql::MySQL_Driver *driver;
Connection *con;
Statement *state;
ResultSet *result;
// 初始化驱动
driver = sql::mysql::get_mysql_driver_instance();
// 建立链接
con = driver->connect("tcp://127.0.0.1:3306", "root", "123");
state = con->createStatement();
state->execute("use test");
// 查询
result = state->executeQuery("select * from testuser where id < 1002");
// 输出查询
while(result->next())
{
int id = result->getInt("ID");
string name = result->getString("name");
cout << id << " : " << name << endl;
}
delete state;
delete con;
}
int _tmain(int argc, _TCHAR* argv[])
{
RunConnectMySQL();
getchar();
return 0;
}
以上是转载的网上的例子
///////////////////////////////////////////////////////////////////////
但在实际的操作过程中,却遇到了新的问题。
当我按照上面的步骤,依次完成Demo版本的检验,但在Debug版本下,却无法使用(VS2008)。
3. 项目属性页->Linker->Input->Additional Dependencies。添加这两项mysqlcppconn.lib,mysqlcppconn-static.lib(mysql/lib目录下的两个.lib文件)
当添加完这两个lib文件后,进行Debug调色运行,当运行到string name = result-getString("name");时出现错误。
是什么原因导致的呢!因为mysqlcppconn.lib或mysqlcppconn-static.lib是Release版本下的,不能做Debug版本的程序。string或vertor在释放内存时会报错。可能在编译时的依赖顺序不一样。
可在项目属性页->Linker->Input->Additional Dependencies。中添加msvcrt.lib msvcprt.lib 两个lib文件。使其支持Debug的调试。
相关文章推荐
- 使用MySQL connector/C++链接MySQL数据库
- 使用MySQL connector/C++链接MySQL数据库
- 使用MySQL connector/C++链接MySQL数据库
- 使用MySQL connector/C++链接MySQL数据库
- 使用MySQL connector/C++链接MySQL数据库
- 使用MySQLConnector/C++连接MySQL数据库
- mysql connector c++与 visual studio 2012 联合使用
- C++使用mysql 库访问MYSQL数据库,mysql_init函数破坏内存的问题
- php使用mysql扩展库链接mysql数据库(查询)
- Linux下安装使用mysql connector/C++
- 使用Connector/C++(VS2015)连接MySQL的完整例子
- Windows下mysql connector c++ 使用setblob后执行报错
- VS 2010中使用MySQL Connector C/C++的问题
- VS2010 64 位使用mysql-connector-c++-noinstall-1.1.3-winx64开发MySQL,供初学者
- c/c++使用VS2013连接MySQL与ubuntu下c链接mysql
- Python3使用mysql.connector操作mysql数据库
- C++使用ODBC链接MYSQL出现乱码问题
- 在CentOS里使用MySQL Connector/C++
- MySQL Connector/C++ 操作MySQL数据库(例程)
- Linux下安装使用 mysql connector/C++