赞
踩
一、官网下载https://dev.mysql.com/downloads/connector/cpp/源码生成需要cmake编译,本人直接下载了生成好的lib、dll、.h文件直接使用:
解压后:
二、配置(vs2017)
1、项目/属性/(C/C++)/常规/附加包含目录 中添加.h文件所在的include文件夹。
2、项目/属性/链接器/常规/附加库目录 加入.lib所在的文件夹。debug和release加入各自对应的文件夹。
3、项目/属性/链接器/输入/附加依赖项 加入.lib文件。
4、将mysqlcppconn-7-vs14.dll拷贝至程序根目录下,还需要将libeay32.dll和ssleay32.dll也拷贝至程序根目录下,否则会报错。
5、程序用到了boost库智能指针,下载boost库,解压后为boost_1_64_0,程序中附加包含目录引入即可,否则会报错。
6、程序引入头文件
- #include "jdbc/mysql_connection.h"
- #include "jdbc/mysql_driver.h"
- #include "jdbc/cppconn/statement.h"
7、连接数据库
- //初始化驱动
- sql::mysql::MySQL_Driver *driver = NULL;
- sql::Connection *con = NULL;
- driver = sql::mysql::get_mysql_driver_instance();
- if (driver == NULL)
- {
- cout << "driver is null" << endl;
- }
- con = driver->connect("tcp://localhost:3306", "root", "root");
- if (con == NULL)
- {
- cout << "conn is null" << endl;
- }
- cout << "connect suceess" << endl;
8、程序使用mysql版本为mysql-5.6.24-win32,完整的代码如下:
- #include "iostream"
- #include "jdbc/mysql_connection.h"
- #include "jdbc/mysql_driver.h"
- #include "jdbc/cppconn/statement.h"
- #include "jdbc/cppconn/prepared_statement.h"
-
- using namespace std;
- using namespace sql;
- int main()
- {
- //初始化驱动
- sql::mysql::MySQL_Driver *driver = NULL;
- sql::Connection *conn = NULL;
- driver = sql::mysql::get_mysql_driver_instance();
- if (driver == NULL)
- {
- cout << "driver is null" << endl;
- }
- //连接
- //con = driver->connect("tcp://localhost:3306", "root", "root");
- conn = driver->connect("tcp://localhost:3306/ourcms", "root", "root");
- if (conn == NULL)
- {
- cout << "conn is null" << endl;
- }
- cout << "connect suceess" << endl;
- //查询
- int flag = 0;
- sql::Statement *stmt = conn->createStatement();
- sql::ResultSet *res;
- res = stmt->executeQuery("SELECT * FROM cms_device");
- while (res->next())
- {
- cout << res->getInt("id") << endl;
- cout << res->getString("phone").c_str() << endl;
- cout << res->getString("imsi").c_str() << endl;
- }
- //插入
- conn->setAutoCommit(0);//关闭自动提交
- PreparedStatement *prep_stmt;
- int updatecount = 0;
- res->first();
- flag = 0;
- while (res->next())
- {
- if (strcmp(res->getString("imsi").c_str(), "460010010000100") == 0)
- {
- flag = 1;
- break;
- }
- }
- if (flag == 0) {
- prep_stmt = conn->prepareStatement("INSERT INTO cms_device (id,phone,imsi) VALUES (111,?,?)");
- prep_stmt->setString(1, "15043214321");
- prep_stmt->setString(2, "460010010000100");
- updatecount = prep_stmt->executeUpdate();
- }
- Savepoint *savept;
- savept = conn->setSavepoint("SAVEPT1");
- res->first();
- flag = 0;
- while (res->next())
- {
- if (strcmp(res->getString("imsi").c_str(), "460010010000101") == 0)
- {
- flag = 1;
- break;
- }
- }
- if (flag == 0) {
- prep_stmt = conn->prepareStatement("INSERT INTO cms_device (phone,imsi) VALUES (?,?)");
- prep_stmt->setString(1, "15043214321");
- prep_stmt->setString(2, "460010010000101");
- updatecount = prep_stmt->executeUpdate();
- }
- conn->rollback(savept);
- conn->releaseSavepoint(savept);
- conn->commit();
- //更新
- conn->setAutoCommit(1);//打开自动提交
- prep_stmt = conn->prepareStatement("update cms_device set phone=? where phone=?");
- prep_stmt->setString(1, "15011111111");
- prep_stmt->setString(2, "15043214321");
- updatecount = prep_stmt->executeUpdate();
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。