本文用的是 mysql 自己的接口来实现数据库的连接,所有没有很强的平台依赖性。(代码亲测,vs2012 x64下编译通过,可以直
接封装成动态库使用)
为了将查询的数据传出数据库,辅助一个 bus 类,该类主要的数据结构是 map 和 list ,这样做能够实现查询功能与数据的有效分离。
另外,本文虽然是数据库的查询功能实现,但是实际上也做了:插入、修改、删除,后三种操作都不需要辅助类,直接修改
Squery 函数就可以轻松满足。
/*
coder:meson
description:
1.STL应用时,char *尽量换成STL 中的string,避免一些编码问题,
2.该类用于存储数据库查询数据集.
date:2018/9/9
*/
#ifndef BUS_H
#define BUS_H
#include <string>
#include <map>
#include <list>
using namespace std;
class bus
{
public:
bus(void);
~bus(void);
public:
//返回值:0 成功,-1 失败
int set(string key,string value);
//返回值:INVALID 不成功
list<string> get(string key);
//返回值:INVALID 不成功 pos从0开始
string get(string key,int pos);
private:
map<string,list<string>> mapA;
};
#endif
#include "bus.h"
bus::bus(void)
{
}
bus::~bus(void)
{
}
int bus::set(string key,string value)
{
int ret=-1;
if((&key!=NULL)&&(&value!=NULL)){
(this->mapA[key]).push_back(value);
ret=0;
}
return ret;
}
list<string> bus::get(string key)
{
map<string,list<string>>::iterator it=this->mapA.find(key);
if(it!=this->mapA.end()){
return it->second;
}
else
{
list<string> temp;
temp.push_back("INVALID");
return temp;
}
}
string bus::get(string key,int pos)
{
string temp;
temp = "INVALID";
map<string,list<string>>::iterator it=this->mapA.find(key);
if(pos<it->second.size()){
if(it!=this->mapA.end()){
int j=0;
list<string>::iterator itb=it->second.begin();
while(j<pos){
itb++;
j++;
}
return *itb;
}
else{
return temp;
}
}
else{
return temp;
}
}
/*
coder:meson
description:
1.将mysql安装目录下的 include文件 添加到项目的C/C++栏下的常规拦下的“附加包含目录”里;
2.将mysql安装目录下的 lib文件 添加到项目的链接器栏下的常规拦下的“附加库目录”里;
3.64bit mysql 需要将VS编译平台换成 x64.
date:2018/9/8
*/
#ifndef SQLCONN_H
#define SQLCONN_H
//mysql所需的头文件和库文件
#include "winsock.h"
#include "mysql.h"
#include "bus.h"
//代替在linker里设置.lib文件,这是微软自己的命令,不是C++的语法
#pragma comment(lib,"libmysql.lib")
//返回值:0 成功,-1 连接失败,-2 查询失败
int Squery(char* query,bus &outbus);
#endif
#include "SqlConn.h"
#include <iostream>
using namespace std;
int Squery(char* query,bus &outbus)
{
//连接MySql数据库
char* host="localhost";
char *user="root";
char *password="ams_120";
char *database="meson";
unsigned int port=3306;
MYSQL conn;
int ret;
try
{
mysql_init(&conn);
if(!mysql_real_connect(&conn, host,user,password,database,port,NULL,0))
{
//cout<<"连接数据库: "<<database<<" 失败!"<<endl;
ret=-1;
return ret;
}
else{
//cout<<"连接数据库: "<<database<<" 成功!"<<endl;
ret=0;
}
}
catch (...)
{
ret=-1;
return ret;
}
//执行查询功能
if (mysql_real_query(&conn,query,(UINT)strlen(query))){
//cout<<"Error making query: "<<mysql_error(&conn)<<endl;
ret=-2;
return ret;
}
//cout<<"query "<<query<<" succeed!"<<endl;
//输出查询结果
MYSQL_RES *res;
MYSQL_ROW row;
int count=0;
res = mysql_store_result(&conn);
while(row=mysql_fetch_row(res)){
MYSQL_FIELD *fields=res->fields;
for(unsigned int t=0;t<mysql_num_fields(res);t++){
outbus.set(fields->name,row[t]);
fields++;
}
count++;
}
/*
***************************************************
cout<<res->fields->name<<"\t";
res->fields++;
cout<<res->fields->name<<"\t";
res->fields++;
cout<<res->fields->name<<"\t"<<endl;
while(row = mysql_fetch_row(res)){
for(unsigned int t=0;t<mysql_num_fields(res);t++){
cout<<row[t]<<"\t";
}
cout<<endl;
count ++;
}
**************************************************
*/
//cout<<"number of rows "<<count<<endl;
//cout<<"mysql_free_result..."<<endl;
mysql_free_result(res);
mysql_close(&conn);
return ret;
}
//测试
#include "SqlConn.h"
#include <iostream>
int main()
{
char *query="select * from student";
bus outbus;
Squery(query,outbus);
list<string> temp=outbus.get("age");
cout<<temp.back()<<endl;
cout<<outbus.get("age",0)<<endl;
system("pause");
return 0;
}
数据库表
查询结果: