58 lines
1.3 KiB
C++
58 lines
1.3 KiB
C++
#pragma once
|
|
#include <functional>
|
|
#include <string>
|
|
|
|
class MySQLResult
|
|
{
|
|
public:
|
|
MySQLResult();
|
|
~MySQLResult();
|
|
|
|
/// 获取数据表的表头数量和结果行数
|
|
unsigned int getNumFields();
|
|
uint64_t getNumRows();
|
|
|
|
/// 获取数据表的表头名称
|
|
char* getFieldName(int Index);
|
|
|
|
/// 对于每行数据执行func
|
|
void stepRow(const std::function<void(char** RowPtr,unsigned long* FieldLength)>& func);
|
|
|
|
private:
|
|
struct impl;
|
|
impl* pimpl;
|
|
|
|
friend class MySQLConn;
|
|
};
|
|
|
|
class MySQLConn
|
|
{
|
|
public:
|
|
MySQLConn();
|
|
~MySQLConn();
|
|
|
|
/// 数据库连接
|
|
/// 返回值: 0:连接正常, -1:连接错误
|
|
int connect(const std::string& host, const std::string& user, const std::string& passwd, const std::string& db, unsigned int port);
|
|
|
|
/// 执行SQL语句
|
|
/// 返回值: 1: 语句执行正常,无返回结果(func未执行) 0:语句执行正常,返回结果集交付func执行. -1:语句执行错误.(mysql_real_query失败) -2: 语句执行正确,获取结果出现错误
|
|
int exec(const std::string& SQLCommand,const std::function<void(MySQLResult&)>& func);
|
|
|
|
/// 获取错误信息
|
|
const std::string& getError();
|
|
unsigned int getErrCode();
|
|
|
|
/// 当结果为空时用于判断数据库运行正常与否
|
|
unsigned int getFieldCount();
|
|
uint64_t getAffectedRows();
|
|
|
|
protected:
|
|
/// 更新错误信息
|
|
void _doUpdateError();
|
|
|
|
private:
|
|
struct impl;
|
|
impl* pimpl;
|
|
};
|