不为失败找借口,只为成功找方法!
分类:
2011-09-11 16:15:31
原文地址:自己实现MySQL CPP语言接口 作者:luozhiyong131
/*
* 自定义mysql c++类
* 使用C++语言简单实现MySQL CPP语言接口
* Lzy 2011-8-16
*/
#include
#include
#include
#include
#include
using namespace std;
typedef map<string, string> Record;
typedef map<string, string>::iterator RecordIter;
typedef vector<Record> Table;
typedef vector<Record>::iterator TableIter;
typedef vector<string> Fields;
typedef vector<string>::iterator FieldsIter;
class MysqlDBAccess
{
public:
MysqlDBAccess(){isSeverConnected = isDBConnected = false;}
~MysqlDBAccess(){if(isSeverConnected) DisconnentSever();}
bool ConnectSever(string severName, string userName, string pwd="", int port=3306); //连接服务器
void DisconnentSever(void){mysql_close(mysql);}
bool ConnectDB(string dbName); //连接数据库
bool ExcuteSQL(string sql); //执行语句
int SelectRecord(string selectSQL); //执行查寻诗句并显示查寻内容
protected:
int GetFields(Fields & fields);
void DisplayTable(Fields fd,Table dt);
private:
MYSQL *mysql;
MYSQL_RES *result;
MYSQL_ROW row;
MYSQL_FIELD *field;
bool isSeverConnected;
bool isDBConnected;
string dbName;
};
/*
* 执行查寻语句,并显示查寻结果
*/
int MysqlDBAccess::SelectRecord(string selectSQL)
{
int rowNum=-1;
Table table;
Fields fields;
Record record;
if (isDBConnected==false) {
cout<<"Not connect to any database"<<endl;
return false;
}
if (ExcuteSQL(selectSQL)==false)
return false;
result = mysql_store_result(mysql);
rowNum = mysql_affected_rows(mysql);
GetFields(fields);
for (int i=1; i<=rowNum; i++) {
row = mysql_fetch_row(result);
for (int j = 0; j<fields.size(); j++) {
if (row[j] == NULL) {
record[fields[j]] = "";
}
else {
record[fields[j]] = row[j];
}
}
table.push_back(record);
record.clear();
}
mysql_free_result(result);
DisplayTable(fields, table);
return true;
}
/*显示查寻的内容*/
void MysqlDBAccess::DisplayTable(Fields fd,Table dt)
{
TableIter iter;
Record rt;
string valStr;
for (iter = dt.begin(); iter != dt.end();iter++) {
rt = *iter;
for (int i = 0; i < fd.size(); i++) {
valStr = rt[fd[i]];
if (valStr != "") {
cout<<valStr<<" ";
}
else {
cout<<"---"<<" ";
}
}
cout<<endl;
}
}
int MysqlDBAccess::GetFields(Fields &fields)
{
while (field = mysql_fetch_field(result)) {
fields.push_back(field->name);
}
return 0;
}
/*执行mysql语句*/
bool MysqlDBAccess::ExcuteSQL(string sql)
{
mysql_set_character_set(mysql, "gb2312");
if (mysql_query(mysql, sql.c_str())) {
cout<<cout<<"mysql_query: "<<mysql_error(mysql)<<endl;
return false;
}
return true;
}
//进入数据库
bool MysqlDBAccess::ConnectDB(string dbName)
{
if (mysql_select_db(mysql, dbName.c_str())) {
cout<<cout<<"mysql_select_db: "<<mysql_error(mysql)<<endl;
return false;
}
isDBConnected = true;
this->dbName = dbName;
return true;
}
bool MysqlDBAccess::ConnectSever(string severName, string userName, string pwd, int port)
{
mysql = mysql_init(NULL);
if(!mysql_real_connect(mysql, severName.c_str(), userName.c_str(), pwd.c_str(), NULL, port, NULL, 0))
{
cout<<"mysql_real_connect: "<<mysql_error(mysql)<<endl;
return false;
}else
isSeverConnected = true;
return true;
}
/*
* 测试代码
*/
int main(void)
{
MysqlDBAccess mysql;
if(mysql.ConnectSever("localhost","root", "123456"))
cout<<"连接成功"<<endl;
mysql.ConnectDB("test");
mysql.ExcuteSQL("create table if not exists lzy(id int, name varchar(10))");
mysql.ExcuteSQL("insert into lzy values(1,'lzy1')");
mysql.ExcuteSQL("insert into lzy values(2,'lzy2')");
mysql.ExcuteSQL("insert into lzy values(3,'lzy3')");
mysql.SelectRecord("select * from lzy");
return 0;
}
源码文件: mysql C++.rar
参考文件: mysql++.rar