#pragma once
#ifndef DB_MYSQL_H
#define DB_MYSQL_H
#include "stdafx.h"
#include <winsock2.h>
#include "include/mysql.h"
#include <map>
#include <vector>
#include <iostream>
using namespace std;
typedef map<CString, CString > strMap;/*key: 字段 value: 值*/
class CDBMYSQL
{
public:
CDBMYSQL(CString szHost, CString szUser, CString szPassword, unsigned int port,CString szCharacter);// 构造函数
CDBMYSQL(); //构造函数
~CDBMYSQL();//析构函数
protected:
MYSQL *m_pMYSQL; //代表一个到数据库的连接
private:
CString m_szHost; //连接的服务器
CString m_szUser; //用户名
CString m_szPassword; //连接密码
CString m_szCharacter;
unsigned int m_iPort; //连接端口
CString m_szDbName; //操作的数据库的名称
MYSQL_RES *m_pResult; //操作的结果
CString m_szQuery; //sql语句
unsigned long m_lNum; //返回查询得到的结果数
CString m_szError; //错误提示信息
unsigned int m_iDebug; //是否显示调试信息
strMap m_Info; //查询语句返回一条结果
vector<strMap> m_vectorInfo; //查询语句可能会返回多条结果
vector<CString> m_vectorFields; //返回查询结果的列
void DisPlayError();
public:
unsigned int DBConnect();//连接数据库
unsigned int DBSelect(CString szDB); //连接一个数据库
void SetQuery(CString szQuery); //设定查询语句
unsigned int DBQuery(); //查询数据库
strMap GetInfo(); //返回查询得到的一条结果
vector<strMap> GetArray(); //返回查询得到的结果
CString GetError(); //返回错误信息
vector<CString> GetFields();//返回查询后的列值
unsigned int InsertData(CString table,strMap *data); //向数据库中插入一条数据
unsigned long GetLastID(); //返回最后一个自动增量的值
unsigned long GetRowNum(); //返回一条sql语句影响的行数
unsigned int UpdateData(CString table,strMap *data,CString condition); //根据条件修改一条数据
unsigned int DeleteData(CString table,CString condition); //根据条件删除数据
};
#endif
哥修改的他人写的一套c++ 操作Mysql ado #include "DBMYSQL.h" CDBMYSQL::CDBMYSQL() { } /*构造函数,设定连接的服务器,用户名,密码和端口*/ CDBMYSQL::CDBMYSQL(CString szHost, CString szUser, CString szPassword, unsigned int iPort=3306, CString szCharacter = "UTF8") { m_pMYSQL = mysql_init(NULL); m_lNum = 0; m_szError=""; m_szQuery=""; m_pResult = NULL; m_szCharacter = szCharacter; CDBMYSQL::m_szHost= szHost; CDBMYSQL::m_szUser= szUser; CDBMYSQL::m_szPassword= szPassword; CDBMYSQL::m_iPort= iPort; CDBMYSQL::m_szCharacter = szCharacter; } /*析构函数*/ CDBMYSQL::~CDBMYSQL() { if(m_pResult != NULL) mysql_free_result(m_pResult); m_vectorFields.clear(); m_szError = ""; m_vectorInfo.clear(); m_szDbName = ""; m_vectorInfo.clear(); mysql_close(m_pMYSQL); } /*连接数据库*/ unsigned int CDBMYSQL::DBConnect() { MYSQL *con; if(m_pMYSQL == NULL) { m_szError = "初始化mysql错误"; return 1; } con = mysql_real_connect(m_pMYSQL, m_szHost.GetBuffer(0), m_szUser.GetBuffer(0), m_szPassword.GetBuffer(0), NULL, m_iPort,NULL,0); if(con == NULL) { m_szError=mysql_error(m_pMYSQL); return mysql_errno(m_pMYSQL); } mysql_set_character_set(m_pMYSQL, m_szCharacter); return 0; } /*选择一个数据库*/ unsigned int CDBMYSQL::DBSelect(CString szDataBase) { unsigned int iRet; if( m_pMYSQL == NULL) return 1; m_szDbName = szDataBase; iRet = mysql_select_db(m_pMYSQL,m_szDbName.GetBuffer(0)); if(iRet != 0) { m_szError += mysql_error(m_pMYSQL); } return iRet; } /*设定sql语句*/ void CDBMYSQL::SetQuery(CString szQuery) { ASSERT(!szQuery.IsEmpty()); if(m_pResult != NULL ) { mysql_free_result(m_pResult); } m_szQuery = szQuery; } /*执行sql语句*/ unsigned int CDBMYSQL::DBQuery() { unsigned int iRet; if( m_pMYSQL == NULL) return 1; ASSERT(!m_szQuery.IsEmpty()); iRet = mysql_query(m_pMYSQL,m_szQuery.GetBuffer()); if(iRet == 0) { m_pResult = mysql_store_result(m_pMYSQL); m_lNum = mysql_affected_rows(m_pMYSQL); m_Info.clear(); m_vectorInfo.clear(); m_vectorFields.clear(); } else { iRet = mysql_errno(m_pMYSQL); m_szError = mysql_error(m_pMYSQL); cout<<m_szError<<endl; } return iRet; } /*获取查询得到的一条结果*/ strMap CDBMYSQL::GetInfo() { MYSQL_ROW row; unsigned int i; ASSERT(m_pMYSQL != NULL); if(m_Info.size() > 0) return m_Info; if(m_pResult != NULL) { GetFields(); row = mysql_fetch_row(m_pResult); if(row != NULL) for(i=0;i<m_vectorFields.size();i++) m_Info[m_vectorFields[i]] = (char*)row[i]; } return m_Info; } /*获取查询得到的所有结果*/ vector<strMap> CDBMYSQL::GetArray() { MYSQL_ROW row; unsigned int i; strMap tmp; ASSERT(m_pMYSQL != NULL); if(m_vectorInfo.size() > 0) return m_vectorInfo; if(m_pResult != NULL) { GetFields(); while(row = mysql_fetch_row(m_pResult)) { if(row != NULL) { for(i=0;i<m_vectorFields.size();i++) { tmp[m_vectorFields[i]] = (char *)row[i]; } m_vectorInfo.push_back(tmp); } } } return m_vectorInfo; } /*获取sql语句执行影响的行数*/ unsigned long CDBMYSQL::GetRowNum() { return m_lNum; } /*获取插入后的id号*/ unsigned long CDBMYSQL::GetLastID() { return mysql_insert_id(m_pMYSQL); } /*向数据库插入数据*/ unsigned int CDBMYSQL::InsertData(CString szTable,strMap *pData) { strMap::const_iterator iter; CString szQuery; int iFlag=0; ASSERT(m_pMYSQL != NULL); ASSERT(!szTable.IsEmpty()); ASSERT(pData != NULL); for(iter = pData->begin(); iter!= pData->end(); iter++) { if(iFlag == 0) { szQuery = "insert into "; szQuery += szTable; szQuery += " set "; szQuery += iter->first; szQuery += " = '"; szQuery += iter->second; szQuery += "'"; iFlag++; } else { szQuery += ","; szQuery += iter->first; szQuery += "= '"; szQuery += iter->second; szQuery += "'"; } } SetQuery(szQuery); return DBQuery(); } /*根据条件修改数据*/ unsigned int CDBMYSQL::UpdateData(CString szTable, strMap *pDataMap, CString szCondition) { strMap::const_iterator iter; CString szQuery; int iFlag = 0; ASSERT(m_pMYSQL != NULL); ASSERT(!szTable.IsEmpty()); ASSERT(pDataMap != NULL); for(iter = pDataMap->begin(); iter!= pDataMap->end(); iter++) { if(iFlag == 0) { szQuery = " update "; szQuery += szTable; szQuery += " set "; szQuery += iter->first; szQuery += " = '"; szQuery += iter->second; szQuery += "'"; iFlag++; } else { szQuery += ","; szQuery += iter->first; szQuery += "='"; szQuery += iter->second; szQuery += "'"; } } if(szCondition.IsEmpty() != 0) { szQuery += " where "; szQuery += szCondition; } SetQuery(szQuery); return DBQuery(); } /*根据条件删除数据*/ unsigned int CDBMYSQL::DeleteData(CString szTable, CString szCondition) { CString szQuery; ASSERT(m_pMYSQL != NULL); ASSERT(!szTable.IsEmpty()); szQuery = " delete from "; szQuery += szTable; if(szCondition!="") { szQuery += " where "; szQuery += szCondition; } SetQuery(szQuery); return DBQuery(); } /*获取返回的错误信息*/ CString CDBMYSQL::GetError() { return m_szError; } /*返回查询后的列值*/ vector<CString> CDBMYSQL::GetFields() { MYSQL_FIELD *field; ASSERT(m_pMYSQL != NULL); if(m_vectorFields.size()>0) return m_vectorFields; while(field = mysql_fetch_field(m_pResult)) m_vectorFields.push_back(field->name); return m_vectorFields; } // 查询 CDBMYSQL *pDbMySQL = new CDBMYSQL("127.0.0.1", "root", "root", 3306, "gbk"); pDbMySQL->DBConnect(); pDbMySQL->DBSelect("demo"); pDbMySQL->SetQuery("select * from customer"); pDbMySQL->DBQuery(); vector<strMap> info = pDbMySQL->GetArray(); for (int i = 0; i < info.size(); i++) { TRACE("username = %s solution =%s ", info[i]["id"], info[i]["username"]); } strMap map; map["username"] = "xs强"; map["company"] = "xxx投资"; pDbMySQL->UpdateData("customer",&map,"where id = 1"); map["id"] = "3"; map["username"] = "邵学成"; map["company"] = "xzxxx投资有限公司"; map["solutation"] = "先生"; map["phone"] = "136401273005"; map["mobile"] = "13640123005"; map["source"] = "同事"; map["sdate"] = "2011-11-1"; map["job"] = "程序员"; map["web"] = "71945221120"; map["email"] = "topxuh12enghua1@163.com"; map["createtime"] = "2010-11-04 21:11:59"; map["modifiedtime"] = "2010-11-04 21:11:59"; map["note"] = "一sx"; pDbMySQL->InsertData("customer",&map); pDbMySQL->DeleteData("customer"," id=1"); |