最近在学习数据库的内容,起先是在windows下用mysql c++ connector进行编程,之所以选用c++而不是c的api,主要是考虑到c++ connector是按照JDBC的api进行实现的,使用比较方便。废话不多说,直接上代码。代码主要是对常用操作的测试与封装,包括DDL、DQL、DML,以及文本、图像的读写方法

/*
* SQLWork.cpp
*
* Created on: Dec 29, 2016
* Author: tla001
*/ #include "SQLWork.h" SQLWork::SQLWork(string url,string user,string passwd) {
// TODO Auto-generated constructor stub
turl=url;
tuser=user;
tpasswd=passwd;
}
SQLWork::~SQLWork() {
// TODO Auto-generated destructor stub
if(res)
delete res;
if(stmt)
delete stmt;
if(con)
delete con;
} int SQLWork::doConnect(){
try{
dirver = get_driver_instance(); //连接数据库
con = dirver->connect(turl, tuser, tpasswd);
con->setClientOption("characterSetResults", "utf8");
}catch(SQLException &e){
cout << "ERROR: " << e.what();
cout << " (MySQL error code: " << e.getErrorCode();
cout << ", SQLState: " << e.getSQLState() << ")" << endl; if (e.getErrorCode() == ) {
/*
Error: 1047 SQLSTATE: 08S01 (ER_UNKNOWN_COM_ERROR)
Message: Unknown command
*/
cout << "\nYour server does not seem to support Prepared Statements at all. ";
cout << "Perhaps MYSQL < 4.1?" << endl;
} return EXIT_FAILURE;
} catch (runtime_error &e) {
cout << "ERROR: " << e.what() << endl; return EXIT_FAILURE;
} return EXIT_SUCCESS;
}
int SQLWork::changeCharacter(string chars){
try{
con->setClientOption("characterSetResults", chars);
}catch(SQLException &e){
cout << "ERROR: " << e.what();
cout << " (MySQL error code: " << e.getErrorCode();
cout << ", SQLState: " << e.getSQLState() << ")" << endl; if (e.getErrorCode() == ) {
/*
Error: 1047 SQLSTATE: 08S01 (ER_UNKNOWN_COM_ERROR)
Message: Unknown command
*/
cout << "\nYour server does not seem to support Prepared Statements at all. ";
cout << "Perhaps MYSQL < 4.1?" << endl;
} return EXIT_FAILURE;
} catch (runtime_error &e) {
cout << "ERROR: " << e.what() << endl; return EXIT_FAILURE;
} return EXIT_SUCCESS;
}
int SQLWork::chooseDatabase(string db){
try{
con->setSchema(db);
stmt = con->createStatement(); //从表中获取所有信息
}catch(SQLException &e){
cout << "ERROR: " << e.what();
cout << " (MySQL error code: " << e.getErrorCode();
cout << ", SQLState: " << e.getSQLState() << ")" << endl; if (e.getErrorCode() == ) {
/*
Error: 1047 SQLSTATE: 08S01 (ER_UNKNOWN_COM_ERROR)
Message: Unknown command
*/
cout << "\nYour server does not seem to support Prepared Statements at all. ";
cout << "Perhaps MYSQL < 4.1?" << endl;
} return EXIT_FAILURE;
} catch (runtime_error &e) {
cout << "ERROR: " << e.what() << endl; return EXIT_FAILURE;
} return EXIT_SUCCESS;
} int SQLWork::doWork(){
try{
/*
* 执行具体工作
*/
//buildTest();
//operateTable();
//operateData();
//operateDataPre();
//queryData();
queryDataPre(); }catch(SQLException &e){
cout << "ERROR: " << e.what();
cout << " (MySQL error code: " << e.getErrorCode();
cout << ", SQLState: " << e.getSQLState() << ")" << endl; if (e.getErrorCode() == ) {
/*
Error: 1047 SQLSTATE: 08S01 (ER_UNKNOWN_COM_ERROR)
Message: Unknown command
*/
cout << "\nYour server does not seem to support Prepared Statements at all. ";
cout << "Perhaps MYSQL < 4.1?" << endl;
} return EXIT_FAILURE;
} catch (runtime_error &e) {
cout << "ERROR: " << e.what() << endl; return EXIT_FAILURE;
} return EXIT_SUCCESS;
} void SQLWork::buildTest(){
res = stmt->executeQuery("SELECT * from student"); //循环遍历
while (res->next()) { //输出,id,name,age字段的信息
cout << res->getInt("id")
<< " | " << res->getString()
<< " | " << res->getInt("tage")
<< " | " << res->getString("thome")
<< endl;
} //清理
} /*
* 执行DDL语句Data Definition Language 数据定义语言
*/
void SQLWork::operateTable(){
//string sqlOp="create table contact(id int primary key auto_increment,name varchar(20),gender varchar(2))";
//string sqlOp="drop table contact";
//string sqlOp="ALTER TABLE contact ADD home VARCHAR(30)";
//string sqlOp="ALTER TABLE contact drop home";
//string sqlOp="ALTER TABLE contact MODIFY gender VARCHAR(4)";
//string sqlOp="ALTER TABLE contact CHANGE thome home VARCHAR(20);";
string sqlOp="CREATE TABLE longtest(id INT PRIMARY KEY AUTO_INCREMENT,content LONGTEXT,img LONGBLOB)"; int count=stmt->executeUpdate(sqlOp);
cout<<"count "<<count<<endl;
} /*
* DML(Data Manipulation Language 数据操控语言)
*/
void SQLWork::operateData(){
//string sqlOp="INSERT INTO contact VALUES(2,'李','男','shenyang')";
//string sqlOp="insert contact(name,gender) values('王二丫','女')";
//string sqlOp="DELETE FROM contact WHERE id>2 and id<5";
string sqlOp="UPDATE contact SET name='韩' WHERE id=2;";
//string sqlOp="";
//string sqlOp=""; int count=stmt->executeUpdate(sqlOp);
cout<<"count "<<count<<endl;
}
void SQLWork::operateDataPre(){
//预编译插入
// string sqlOp="insert contact(name,gender) values(?,?)";
// pstmt=con->prepareStatement(sqlOp);
// pstmt->setString(1,"李四");
// pstmt->setString(2,"男"); //预编译修改
// string sqlOp="UPDATE contact SET name=? WHERE id=?";
// pstmt=con->prepareStatement(sqlOp);
// pstmt->setString(1,"王二");
// pstmt->setInt(2,1); //预编译删除
// string sqlOp="DELETE FROM contact WHERE id>?";
// pstmt=con->prepareStatement(sqlOp);
// pstmt->setInt(1,4); // int count=pstmt->executeUpdate();
// cout<<"count "<<count<<endl; /***********************文本数据*****************************************/
//保存大文本数据(longtext)
// string sqlOp="insert longtest(content) values(?)";
// pstmt=con->prepareStatement(sqlOp);
// ifstream file;
// file.open("./file.txt");
// if(!file){
// cout<<"open failed"<<endl;
// exit(-1);
// }
// pstmt->setBlob(1,&file);
//
// int count=pstmt->executeUpdate();
// cout<<"count "<<count<<endl;
// file.close(); /***********************二进制数据*****************************************/
//保存二进制数据(longblob)
string sqlOp="insert longtest(img) values(?)";
pstmt=con->prepareStatement(sqlOp);
ifstream file;
file.open("./test.png",ios::binary);
if(!file){
cout<<"open failed"<<endl;
exit(-);
}
pstmt->setBlob(,&file); int count=pstmt->executeUpdate();
cout<<"count "<<count<<endl;
file.close();
}
/*
* 数据查询语句DQL
*/
void SQLWork::queryData(){
string sqlOp="SELECT * FROM contact";
//string sqlOp="";
//string sqlOp=""; res = stmt->executeQuery(sqlOp); //循环遍历
while (res->next()) {
int id=res->getInt("id");
string name=res->getString("name");
string gender=res->getString("gender");
string home=res->getString("home");
cout<<"id: "<<id<<"\t| name: "<<name<<"\t| gender: "<<gender<<"\t| home: "<<home<<endl;
}
} void SQLWork::queryDataPre(){
//预编译查询
// string sqlOp="SELECT * FROM contact";
// //string sqlOp="";
// //string sqlOp="";
// pstmt=con->prepareStatement(sqlOp);
// res = pstmt->executeQuery();
// while (res->next()) {
// int id=res->getInt("id");
// string name=res->getString("name");
// string gender=res->getString("gender");
// string home=res->getString("home");
// cout<<"id: "<<id<<"\t| name: "<<name<<"\t| gender: "<<gender<<"\t| home: "<<home<<endl;
// } /***********************文本数据*****************************************/
//查询大文件数据
// string sqlOp="SELECT * FROM longtest";
//
// pstmt=con->prepareStatement(sqlOp);
// res = pstmt->executeQuery();
// while (res->next()) {
// string content=res->getString("content");
// cout<<content<<endl;
// }
/***********************二进制数据*****************************************/
//查询二进制数据
string sqlOp="SELECT * FROM longtest"; pstmt=con->prepareStatement(sqlOp);
res = pstmt->executeQuery();
int i=;
while (res->next()) {
i++;
ostringstream iname;
iname <<"img"<<i<<".png";
istream *is=res->getBlob("img");
if(is->peek()>){
ofstream file(iname.str().c_str(),ios::binary);
char data[];
while(is->read(data,)){
file.write(data,sizeof(data));
}
file.write(data,sizeof(data));
file.close();
}
}
}

获取整个工程GitHub

Linux下MySQL c++ connector示例的更多相关文章

  1. linux 下mysql的启动 、调试、排错

    Linux 下 MySQL 启动与关闭 说明 一.启动 1.1  MySQL 进程 可以用ps 命令查看进程: [root@rac2 ~]# ps -ef|grep mysql root     21 ...

  2. linux下MySQL安装登录及操作

    linux下MySQL安装登录及操作 二.安装Mysql 1.下载MySQL的安装文件 安装MySQL需要下面两个文件: MySQL-server-4.0.16-0.i386.rpm MySQL-cl ...

  3. Linux下MySQL数据库的备份与恢复

    Linux下MySQL数据库的备份与恢复 作者:Grey 原文地址: Github 语雀 博客园 基于版本 MySQL5.7 Deepin Linux 15.11 xtrabackup-2.4.18 ...

  4. Linux下MySQL忘记密码

    系统:CentOS6.6 64位 参考文档(截图请看原网址): Linux下MySQL忘记root密码怎么办_百度经验 http://jingyan.baidu.com/article/1709ad8 ...

  5. linux下mysql远程访问

    // */ // ]]>   linux下mysql远程访问 Mysql默认root用户只能本地访问,不能远程连接管理mysql数据库,但项目中必须要远程导 入导出数据,所以研究了一下. Tab ...

  6. linux下mysql字符集编码问题的修改

    安装完的MySQL的默认字符集为 latin1 ,为了要将其字符集改为用户所需要的(比如utf8),就必须改其相关的配置文件:由于linux下MySQL的默认安装目录分布在不同的文件下:不像windo ...

  7. Linux下MySQL的备份与还原

    Linux下MySQL的备份与还原 1. 备份 [root@localhost ~]# cd /var/lib/mysql (进入到MySQL库目录,根据自己的MySQL的安装情况调整目录) [roo ...

  8. linux下mysql定时备份数据库

    linux下mysql定时备份数据库 (2010-10-21 12:40:17) 转载▼ 标签: 杂谈   一.用命令实现备份 首页进入mysql的bin目录 1.备份数据#mysqldump -uu ...

  9. 设置Linux下Mysql表名不区分大小写

    1.Linux下mysql安装完后是默认:区分表名的大小写,不区分列名的大小写:2.用root帐号登录后,在/etc/my.cnf中的[mysqld]后添加添加lower_case_table_nam ...

随机推荐

  1. COGS 898. [咲 -Saki-] 天才麻将少女什么编

    ★☆   输入文件:sakinani.in   输出文件:sakinani.out   简单对比时间限制:1 s   内存限制:256 MB 题目背景 二十一世纪,世界上的麻将竞技人数超过一亿,日本每 ...

  2. .net reflector 的缺陷

    .net reflector是一个反编译DLL的工具,在安装后如果电脑上有VS也会同时安装到VS里面,但是他是收费的,虽然反编译的效果很好,但是运行VS2013时(或许其他版本也有这样的问题)如果项目 ...

  3. POI对EXCEL的操作【重点:如何设置CELL格式为文本格式】

    实际开发过程中通常用到的就是从数据库导出EXCEL表格了,JXL可以这样做,其实POI也可以(关于JXL与POI的异同可访问我之前总结的文章),之前写过POI对七种文档(当然也包括EXCEL)的内容读 ...

  4. World Wind Java开发之六——解析shape文件(转)

    http://blog.csdn.net/giser_whu/article/details/41647117 最近一直忙于导师项目的事情了,几天没更新了,昨天和今天研究了下WWJ解析shp文件的源代 ...

  5. opencv与灰度图

    https://blog.csdn.net/qq_32211827/article/details/56854985 首先,灰度图可以是一个通道存成图片,也可以是3个通道存成图片,3个通道存成图片,其 ...

  6. Bootstrap 历练实例-轮播(carousel)插件的事件

    事件 下表列出了轮播(Carousel)插件中要用到的事件.这些事件可在函数中当钩子使用. 事件 描述 实例 slide.bs.carousel 当调用 slide 实例方法时立即触发该事件. $(' ...

  7. React后台管理系统-商品列表搜索框listSearch组件

    1.商品列表搜索框 2.搜索框页面的结构为 <div className="row search-wrap">               <div classN ...

  8. Git笔记(流水账)

    命令git checkout -- readme.txt意思就是,把readme.txt文件在工作区的修改全部撤销,这里有两种情况: 一种是readme.txt自修改后还没有被放到暂存区,现在,撤销修 ...

  9. python字符串处理方法

    一.combine & duplicate 字符串结合和复制 字符和字符串可以用来相加来组合成一个字符串输出: 字符或字符串复制输出. 二.Extract &Slice 字符串提取和切 ...

  10. Volatile小结

    1)Java 中能创建 Volatile 数组吗? 能,Java 中可以创建 volatile 类型数组,不过只是一个指向数组的引用,而不是整个数组.我的意思是,如果改变引用指向的数组,将会受到 vo ...