使用官方提供的连接API , 支持多平台 可见链接C++ API下载高版本的需要boots环境, 比较麻烦, 建议使用低版本 . 我使用的是 Connector/C++ 1.0.5 下载得到压缩包 会得到 include 与 lib , 新建consolo项目 包含文件目录 include 路劲 与 附加链接 lib项 我顺着官方文档写, 项目载入头文件 #include <mysql_connection.h> #include <mysql_driver.h>连接数据库例子 我发现该文章内
sql::mysql::MySQL_Driver *driver;
sql::Connection *con;
driver = sql::mysql::MySQL_Driver::get_mysql_driver_instance();
con = driver->connect("tcp://127.0.0.1:3306", "user", "password");
delete con;
编译后发现 get_mysql_driver_instance 并没有在MySQL_Driver 定义 , 并不是MySQL_Driver成员, 经过搜索引擎我找到 get_mysql_driver_instance并非MySQL_Driver成员 而是 mysql 成员. 所以需要改成
sql::mysql::MySQL_Driver *driver;
sql::Connection *con;
driver = sql::mysql::get_mysql_driver_instance();
con = driver->connect("tcp://127.0.0.1:3306", "user", "password");
delete con;
经过我的测试 编译正常 , 但是却运行时会崩溃在 driver->connect 中,. 连接时程序崩溃. 原来是连接参数填错, 所以这里说明 连接服务器参数不正确会导致程序崩溃. 这是因为低版本的问题吗? 参数填写正常后开始进行查询语句 , 查询语句需要使用sql::Statement 类 需要载入头文件 #include <cppconn/statement.h>
//需要载入 #include <cppconn/statement.h>
sql::Statement *stmt;
stmt = con->createStatement();
stmt->execute("use test"); //SQL查询语句, 选择test库
stmt->execute("INSERT INTO a(t) VALUES (1)"); //插入语句
获取查询集
sql::ResultSet *res;
res = stmt->executeQuery("SELECT t FROM a WHERE t=1 ORDER BY t ASC");
//通过 res->next() 进行获取下一位 规划好集
while (res->next()) {
cout << "id = " << res->getInt(1) << endl;
//cout << ", label = '" << res->getString("label") << "'" << endl;
}
官方还提供了一个类 进行插入数据
sql::Connection *con;
sql::PreparedStatement *prep_stmt;
//省略一堆连接
prep_stmt = con->prepareStatement("INSERT INTO test(id, label) VALUES (?, ?)");
//可见value 中的? 号 , 再看一下PreparedStatement 类所提供的成员函数
prep_stmt->setInt(1, 1);
prep_stmt->setString(2, "a");
prep_stmt->execute();
prep_stmt->setInt(1, 2);
prep_stmt->setString(2, "b");
prep_stmt->execute();
//可见setInt中的两个参数 第一参数为 ? 位置 第二参数为值 随后使用execute 成员函数完成一条查询记录, 并且不需要prepareStatement 再新建一条SQL , 可以继续使用查询
下面例子
#include <stdio.h>
#include <mysql_connection.h>
#include <mysql_driver.h>
#include <cppconn/statement.h>
#include <cppconn/exception.h>
#include <iostream>
#pragma comment(lib, "mysqlcppconn.lib")
using namespace std;
int main(void){
try {
sql::mysql::MySQL_Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
driver = sql::mysql::get_mysql_driver_instance();
con = driver->connect("tcp://127.0.0.1:3306", "root", "pass");
stmt = con->createStatement();
stmt->execute("use test");
stmt->execute("INSERT INTO a(t) VALUES (2)");
sql::ResultSet *res;
res = stmt->executeQuery("SELECT t FROM a WHERE t=1 ORDER BY t ASC");
while (res->next()) {
cout << "id = " << res->getInt(1) << endl;
//cout << ", label = '" << res->getString("label") << "'" << endl;
}
delete con;
}
catch (sql::SQLException &e) {
cout << "# ERR: SQLException in " << __FILE__;
cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
cout << "# ERR: " << e.what();
cout << " (MySQL error code: " << e.getErrorCode();
cout << ", SQLState: " << e.getSQLState() << " )" << endl;
}
printf("test");
getchar();
return 0;
}
可见上面本人的例子 , 需要使用try 操作SQL类 否则一旦SQL语句错误 程序就会崩溃 , connect 中连接错误 也会出错 . 例子通过Statement对象去查询语句 , 使用 execute成员函数, 通过SQL局域 use选择进入test数据库 , 进行插入数据 INSERT INTO 数据表(字段1,字段2) VALUE (字段1值,字段2值) ; 进行一个插入SQL语句 , SQL语句不多讲 通过查询数据 使用ResultSet 类 依然使用 execute Query 进行一个查询 SELECT 字段1,字段2 FORM 数据表 WHERE 条件 ORDER 排序 . SQL完成查询后 (⊙o⊙)….. 我也没看过executeQuery 将值如何处理 , 使用res->next 得到下一个数据集 , 就像文件指针一样
0 条评论