头文件
#ifndef _MYMYSQL_H
#define _MYMYSQL_H
#include <string>
#include "mysql.h"
using namespace std;
class MyMysql
{
protected:
bool m_hasRead = false;
int m_curReadValueIndex = -1;
int m_affected_rows = -1;
string **m_execResult;
int m_rowCount;
int m_colCount;
MyMysql();
public:
static MyMysql *m_pInstance;
MYSQL *m_dbHandle = nullptr;
static MyMysql *NewInstance();
bool connectToDatabase(const char *hostName, const char *user, const char *passwd,
const char *dbname);
bool exec(const string &s);
bool next();
string values(int index);
int rowCount();
int columnCount();
void mysql_read();
int databaseClose();
public:
~MyMysql();
};
#endif
源文件
#include "MyMysql.h"
#include <cstring>
#include <iostream>
using namespace std;
MyMysql *MyMysql::m_pInstance = new MyMysql();
MyMysql::MyMysql() { this->m_dbHandle = mysql_init(NULL); }
MyMysql::~MyMysql() { mysql_close(this->m_dbHandle); }
MyMysql *MyMysql::NewInstance() { return m_pInstance; }
bool MyMysql::connectToDatabase(const char *hostName, const char *user,
const char *passwd, const char *dbname)
{
if (mysql_real_connect(this->m_dbHandle, hostName, user, passwd, dbname, 0,
NULL, 0))
{
mysql_query(m_dbHandle, "set names utf8");
return true;
}
return false;
}
bool MyMysql::exec(const string &s)
{
m_hasRead = false;
m_affected_rows = -1;
m_curReadValueIndex = -1;
return (mysql_query(this->m_dbHandle, s.c_str()) == 0);
}
bool MyMysql::next()
{
if (!m_hasRead)
{
mysql_read();
m_hasRead = true;
m_affected_rows = mysql_affected_rows(this->m_dbHandle);
}
return (++m_curReadValueIndex < m_affected_rows);
}
string MyMysql::values(int index)
{
string ret;
if (m_curReadValueIndex < m_affected_rows)
{
ret = this->m_execResult[m_curReadValueIndex][index];
}
return ret;
}
int MyMysql::rowCount()
{
if (m_affected_rows == -1)
{
mysql_read();
m_hasRead = true;
m_affected_rows = mysql_affected_rows(this->m_dbHandle);
}
return m_affected_rows;
}
int MyMysql::columnCount()
{
return m_colCount;
}
void MyMysql::mysql_read()
{
if (this->m_rowCount != 0)
{
for (int i = 0; i < this->m_rowCount; i++)
{
delete[] this->m_execResult[i];
}
delete[] this->m_execResult;
}
MYSQL_RES *result = mysql_store_result(this->m_dbHandle);
if (result != nullptr)
{
MYSQL_ROW row;
int cow_lenth = result->field_count;
int row_lenth = result->row_count;
this->m_execResult = new string *[row_lenth];
for (int i = 0; i < row_lenth; i++)
{
this->m_execResult[i] = new string[cow_lenth];
}
this->m_rowCount = row_lenth;
this->m_colCount = cow_lenth;
for (int i = 0; i < row_lenth; i++)
{
row = mysql_fetch_row(result);
for (int j = 0; j < cow_lenth; j++)
{
if (row[j])
{
this->m_execResult[i][j] = row[j];
}
else
{
this->m_execResult[i][j] = "";
}
}
}
mysql_free_result(result);
}
}
int MyMysql::databaseClose()
{
mysql_close(m_dbHandle);
return 0;
}
CMake链接方法
include_directories(
/usr/include/mysql
)
link_directories(
/usr/lib/x86_64-linux-gnu
)
target_link_libraries(target libmysqlclient.so)
示例代码
#include "mysql.h"
#include <stdio.h>
#include <string.h>
#include <string>
#include <iostream>
#include <cstring>
#include "MyMysql.h"
#include <iostream>
using namespace std;
int main()
{
MyMysql* pInstall = MyMysql::NewInstance();
pInstall->connectToDatabase("IP地址","用户名", "密码", "数据库名");
pInstall->exec("select * from 表名");
cout << "行数:" << pInstall->rowCount() << endl;
cout << "列数:" << pInstall->columnCount() << endl;
while(pInstall->next())
{
for(int i = 0;i < pInstall->columnCount();++i)
{
cout << pInstall->value(i) << " ";
}
cout << endl;
}
return 0;
}