C++ MySQL封装类
#ifndef MYSQL_MANAGER_H
#define MYSQL_MANAGER_H
#include <Winsock2.h>
#include "mysql.h"
#include <string>
#include <vector>
#include <map>
using namespace std; class mysql_db
{
public:
mysql_db();
~mysql_db();
public:
/*
mysql_open()
return : 1 OK
-1 失败
*/
int mysql_open(const char * host, const char * user, const char * password, const char * database, unsigned int port );
/*
mysql_noResult_query();
非select语句查询
return >0 成功, 为受影响的行数
-1 失败
*/
int mysql_noResult_query(const char * sql );
/*
mysql_select_query();
有结果集的查询
return >0 ok 返回结果集条数
-1 失败
map_results first = 行 second = values
*/
int mysql_select_query(const char * sql, map<int,vector<string>> & map_results);
/*
mysql_select_SingleLine_query();
只有一条数据 , 或者只有一个字段 N 条的查询. 直接调用vector即可
*/
int mysql_select_SingleLine_query(const char * sql, vector<string> & v_results);
/*
mysql_lasterror();
返回最近一次错误信息
*/
string mysql_lasterror();
private:
MYSQL sqlCon;
MYSQL_RES *m_pResult;
MYSQL_ROW m_Row;
}; #endif
#include "stdafx.h"
#include "managesql.h" mysql_db::mysql_db()
{
mysql_init(&sqlCon);// mysql 初始化
}
mysql_db::~mysql_db()
{
mysql_close(&sqlCon);// 关闭连接
} int mysql_db::mysql_open(const char * host, const char * user, const char * password, const char * database, unsigned int port)
{
char nvalue = ;
mysql_options(&sqlCon, MYSQL_OPT_RECONNECT, (char *)&nvalue);// 断线自动重连
mysql_options(&sqlCon, MYSQL_SET_CHARSET_NAME, "gbk");// set name gbk 中文乱码问题
if (!mysql_real_connect(&sqlCon, host, user, password, database, port, NULL, )){
return -;
}
return ;
} int mysql_db::mysql_noResult_query(const char * sql)
{
if (mysql_query(&sqlCon, sql) != ){
return -;
}
return (int)mysql_affected_rows(&sqlCon);
} int mysql_db::mysql_select_query(const char * sql, map<int, vector<string>> & map_results)
{
if (mysql_query(&sqlCon, sql) != ){
return -;
} if(!(m_pResult = mysql_use_result(&sqlCon))){
return -;
}
int i = ; int count = mysql_num_fields(m_pResult);
while(m_Row = mysql_fetch_row(m_pResult)){
vector<string> vVal;
for (int j = ; j < count; j++){
vVal.push_back(m_Row[j]);
}
map_results[i++] = vVal;
}
mysql_free_result(m_pResult);
return i;
} int mysql_db::mysql_select_SingleLine_query(const char * sql, vector<string> & v_results)
{
if (mysql_query(&sqlCon, sql) != ){
return -;
}
if(!(m_pResult = mysql_use_result(&sqlCon))){
return -;
}
int i = ; int count = mysql_num_fields(m_pResult);
while(m_Row = mysql_fetch_row(m_pResult)){
for (int j = ; j < count; j++){
v_results.push_back(m_Row[j]);
}
}
mysql_free_result(m_pResult);
return i;
} string mysql_db::mysql_lasterror()
{
return mysql_error(&sqlCon);
}
// ConsoleApplication2.cpp : Defines the entry point for the console application.
// #include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include "ManageSQL.h"
#include <vld.h>
using namespace std; int main()
{
mysql_db mydb; //打开.. 连接
if (- == mydb.mysql_open("Localhost", "root", "", "electronicpolice", MYSQL_PORT))
cout << mydb.mysql_lasterror() << endl; //创建一个库
string sql = "create database if not exists database_test";
if ( - == mydb.mysql_noResult_query(sql.c_str()))
cout << mydb.mysql_lasterror() << endl; //use 库
sql = "use database_test";
if ( - == mydb.mysql_noResult_query(sql.c_str()))
cout << mydb.mysql_lasterror() << endl; //创建一张表
sql = "create table if not exists table_test(id int not null auto_increment, name varchar(20) not null, age int not null ,primary key (id) )";
if ( - == mydb.mysql_noResult_query(sql.c_str()))
cout << mydb.mysql_lasterror() << endl; //插入2条数据
sql = "insert into table_test (name,age) values('newsoul','25')";
if ( - == mydb.mysql_noResult_query(sql.c_str()))
cout << mydb.mysql_lasterror() << endl;
sql = "insert into table_test (name,age) values('百度','100')";
if ( - == mydb.mysql_noResult_query(sql.c_str()))
cout << mydb.mysql_lasterror() << endl; //mysql_select_SingleLine_query 演示
sql = "select * from table_test where id=1 ";
vector<string> vResults;
if (- == mydb.mysql_select_SingleLine_query(sql.c_str(),vResults))
cout << mydb.mysql_lasterror() << endl; //遍历结果集
cout << "select * from table_test where id=1的结果: " << endl;
for (vector<string>::iterator it = vResults.begin(); it != vResults.end(); it++)
cout << (*it) << endl; cout << endl << endl;
//mysql_select_query 演示
sql = "select * from table_test"; int i = ;
while (i++ < ){
map<int,vector<string>> map_res;
if (- == mydb.mysql_select_query(sql.c_str(),map_res))
cout << mydb.mysql_lasterror() << endl; //遍历...
cout << "select * from table_test的结果:" << endl;
for (map<int,vector<string>>::iterator it = map_res.begin(); it != map_res.end(); it++){
cout << "第" << it->first+<< "条 : ";
for (int i=;i<it->second.size();i++)
{
cout << it->second[i] << "\t";
}
cout << endl;
}
} system("pause");
return ;
}
C++ MySQL封装类的更多相关文章
- 简单的mysql封装类
class mysql{ private $host; private $user; private $pwd; private $dbname; private $charset; private ...
- mysql封装类
<?php ; ; $cnt = mysql_num_rows($rsPtr); ; ) { $id = ...
- 【Python】python3.6 操作mysql
1.首先安装mysqldb 下载 mysqldb 使用命令 pip3 install mysqlclient-1.3.12-cp36-cp36m-win32.whl 下载地址: mysqlclient ...
- PHP之面向对象PHP之面向对象(面向对象是什么)
PHP之面向对象(面向对象是什么) 一.总结 一句话总结: 面向对象就是类:类都要 属性 和 方法 比如人:属性比如身高体重,方法比如吃饭喝水 面向对象中 ,方法即是函数 : 属性即是变量 ,只是面相 ...
- 资源(1)----封装类(连接数据库mysql,分页)
一,链接MYSQL数据库 class DBDA{ public $host="localhost";//服务器地址 public $uid="root";//数 ...
- C# 应用 - 封装类访问 Mysql 数据库
个人经历的项目主要都是用 Postgresql 或 Oracle 数据库,本文非原创,从他处整理而来. 1. 库类 mysql.data.dll using MySql.Data.MySqlClien ...
- mysql 数据库封装类:返回索引、关联、字符串数组;分页查询封装类 :$page=new Page(表的总条数,每页的条数);$sql = "".$page->limit; echo $page->fpage();
<?php class czy { public $host="localhost"; //地址 public $uid="root"; //用户名 pu ...
- MYSQL数据库封装类
<?phpclass DBDA{ public $host="localhost"; public $uid="root"; publi ...
- php--学习封装类 (一)(操作mysql数据库的数据访问)
<?php class DBDA //定义一个类 { //定义成员变量,不能直接定义,前面要加上public或者是private public $host = "localhost&q ...
随机推荐
- ASP.NET Core的身份认证框架IdentityServer4--入门
ASP.NET Core的身份认证框架IdentityServer4--入门 2018年08月11日 10:09:00 qq_42606051 阅读数 4002 https://blog.csdn ...
- Linux centos7VMware Apache和PHP结合、Apache默认虚拟主机
一.Apache和PHP结合 httpd主配置文件/usr/local/apache2.4/conf/httpd.conf 启动报错 [root@davery ~]# /usr/local/apach ...
- XML规范化(DTD)
无意义的XML 之前说过因为xml没有预设的标签,所以说你怎麽写他一般都不会报错. 所以需要对xml的书写格式进行一些限制,这就引入了DTD 下面的这个xml你可以给book添加各种属性还不会报错,但 ...
- 笔记||Python3进阶之装饰器
# 装饰器# 特征: 是用一个@开头的字符串# 装饰器通常用来装饰函数.或者类的方法# 被装饰后的函数,通常是在原有的函数基础上,会多出增加一点功能# 一般来说装饰器本身也是一个函数## def te ...
- 算法设计与分析 - AC 题目 - 第 2 弹
PTA-算法设计与分析-AC原题7-1 最大子列和问题 (20分)给定K个整数组成的序列{ N1, N2, ..., NK },“连续子列”被定义为{ Ni, Ni+1, ..., Nj },其中 1 ...
- 【读书圈】win7 定时发送OA邮件
因为win7任务计划本身xls邮件调用有问题,会显示只读权限 我用vbs脚本替代了它的邮件功能!(我现在对vbs的CDO概念也没大弄清,还不知道需不需要外网,等我找台别的内网机器试试) (另外我试验了 ...
- java中常用的数据结构--Map
一.定义: 将键映射到值的对象. 地图不能包含重复的键; 每个键可以映射到最多一个值. public interface Map<K,V> 请注意!!!, Map 没有继承 Collect ...
- java核心-多线程(6)-线程池-ThreadPoolExecutor
1.java多线程编程少不了使用线程池,线程池相关的工具类所在jdk包,java.util.concurrent 2.使用示例 demo1 public class ThreadPoolDemo { ...
- IdentityServer4专题之二:OpenID介绍
1.基于概念 OpenId是一个以用户为中心的数字身份识别框架,它具有开放.分散.自由等特性.OpenId的创建是基于这样一个概念:我们可以通过URI(或者URL网址)来识别一个网站.同样,我们也可以 ...
- ubuntu 怎么更新?ubuntu更新命令及方法
ubuntu 怎么更新?ubuntu更新命令及方法 安装Ubuntu系统后,第一件事就是更新系统源.由于系统安装的默认源地址在英国,作为Ubuntu的主源,国内连接速度非常慢,所以我们要将它换成就近的 ...