qsql使用接口封装成普通的接口模块,可直接用

对qsql相关操作的模块封装,封装后的接口可以不用熟悉qsql相关类也能使用
// .h头文件
#include 
#include 
#include 
#include 
#include 
class SqlInterface
{
public:
    //SqlInterface();
    //~SqlInterface();

    /* connectionName用于区分不同的连接,既可以是同一个数据库的不同连接,也可以是不同数据库的不同连接,如果为空,则使用默认连接 */
    static bool Open(const QString &dbFileName, const QString &connectionName = "");
    static void RemoveDatabase(const QString &connectionName);

    // 只返回一组值
    static bool Get(const QString &tableName, const QString &fieldName, QVector &valVec,
             const QString &conditionStr, const QString &connectionName = "");

    //返回多组值,返回一组值时也可以调本函数
    static bool Get(const QString &tableName, const QStringList &fieldNameList, QVector > &valVec,
             const QString &conditionStr, const QString &connectionName = "");

    static bool Get(const QString &tableName, const QString &fieldName, QVector &valVec,
             const QMap &conditionMap, const QString &connectionName = "");

    static bool Get(const QString &tableName, const QStringList &fieldNameList, QVector > &valVec,
             const QMap &conditionMap, const QString &connectionName = "");

    static bool Update(const QString &tableName, const QString &fieldName, const QVariant &val, const QString &conditionStr,
                const QString &connectionName = "");

    static bool Update(const QString &tableName, const QStringList &fieldNameList, const QVector &valVec,
             const QString &conditionStr, const QString &connectionName = "");

    static bool Update(const QString &tableName, const QString &fieldName, const QVariant &val,
                const QMap &conditionMap, const QString &connectionName = "");

    static bool Update(const QString &tableName, const QStringList &fieldNameList, const QVector &valVec,
              const QMap &conditionMap, const QString &connectionName = "");

    //一次只允许插入一条记录
    static bool Insert(const QString &tableName, const QStringList &fieldNameList, const QVector &valVec
                , const QString &connectionName = "");

	//一次插入多条语句
	static bool Insert(const QString &tableName, const QStringList &fieldNameList, const QVector > &valVec
		, const QString &connectionName = "");

    static bool Delete(const QString &tableName, const QString &conditionstr, const QString &connectionName = "");

    static bool LoadPixMap(const QString &filepath, QPixmap &pix);
    static bool SavePixMap(const QString &filepath, const QPixmap &pix);

private:
   // static QSqlQuery Query(const QString &tableName, const QString &conditionStr, const QString &connectionName = "");

    static QSqlQuery Query(const QString &tableName, const QStringList &fieldList, const QString &conditionStr,
                    const QString &connectionName = "");

    static QString mapValToString(const QMap &map);
};

// cpp文件
#include 
#include 
#include 
#include 
#include 
#include "SqlInterface.h"

/*
SqlInterface::SqlInterface()
{
   // m_isOpen = false;
}

SqlInterface::~SqlInterface()
{

}*/

/* 需要一个数据库路径,用于打开数据库,一个连接名,连接名用于区分其他的数据库连接 */
bool SqlInterface::Open(const QString &dbFileName, const QString &connectionName)
{
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE",connectionName);
    db.setDatabaseName(dbFileName);
    if(!db.open())
    {
       printf("open db fail\n");
       return false;
    }
    printf("open db succ\n");
    return true;
 /*
    QSqlQuery query;
    bool suc = query.exec("select * from tabletest");
    if(suc)
    {
        while(query.next())
        {
            QSqlRecord recd = query.record();
            printf("count :%d",recd.count());
            for(int i=0; i &valVec,
                       const QString &conditionStr, const QString &connectionName)
{
    QStringList fnlist;
    fnlist< &valVec,
         const QMap &conditionMap, const QString &connectionName)
{
    QString conditionStr = mapValToString(conditionMap);
    return Get(tableName,fieldName,valVec,conditionStr,connectionName);
}

/*  select a,b,c from tablename where e = f

结果返回如:
a   b   c  结果可能有多行,每行用一个vector存储,外面再用一个vector存储所有的行
1   2   3
4   5   6
7   8   9
*/
bool SqlInterface::Get(const QString &tableName, const QStringList &fieldNameList,
                       QVector > &valVec, const QString &conditionStr,
                       const QString &connectionName)
{
    QSqlQuery query = Query(tableName,fieldNameList,conditionStr,connectionName);
    while(query.next())
    {
        QVector rowValVec;
        for(int i=0; i > &valVec,const QMap &conditionMap,
                       const QString &connectionName)
{
    QString conditionStr = mapValToString(conditionMap);
    return Get(tableName,fieldNameList,valVec,conditionStr,connectionName);
}


bool SqlInterface::Update(const QString &tableName, const QString &fieldName, const QVariant &val,
                       const QString &conditionStr, const QString &connectionName)
{
    QString str = QString("update %1 set %2 = ? where %4").arg(tableName).arg(fieldName).arg(conditionStr);
    printf("%s\n",str.toLocal8Bit().data());

    QSqlDatabase db = QSqlDatabase::database(connectionName);
    QSqlQuery query(db);
    query.prepare(str);
    query.bindValue(0,val);
    bool succ = query.exec();
    if(!succ)
    {
        printf("Update fail,err:%s\n",query.lastError().text().toLocal8Bit().data());
        return false;
    }
    return true;
}

bool SqlInterface::Update(const QString &tableName, const QString &fieldName, const QVariant &val,
                          const QMap &conditionMap, const QString &connectionName)
{
    QString conditionStr = mapValToString(conditionMap);
    return Update(tableName, fieldName, val, conditionStr,connectionName);
}


/* fieldNameVec的大小必须跟 valVec的大小一样 */
bool SqlInterface::Update(const QString &tableName, const QStringList &fieldNameList,
                       const QVector &valVec, const QString &conditionStr, const QString &connectionName)
{
    if(fieldNameList.size() == 0 || valVec.size() == 0)
    {
        printf("Update field or value is null\n");
        return false;
    }

    if(fieldNameList.size() != valVec.size())
    {
        printf("Update fieldNameVec.size() != valVec.size()\n");
        return false;
    }

    QString fieldstr = fieldNameList[0] + " = ?";
    for(int i=1; i &valVec,
          const QMap &conditionMap, const QString &connectionName)
{
    QString conditionStr = mapValToString(conditionMap);
    return Update(tableName, fieldNameList, valVec, conditionStr,connectionName);
}

bool SqlInterface::Insert(const QString &tableName, const QStringList &fieldNameList,
                          const QVector &valVec, const QString &connectionName)
{
    if(fieldNameList.size() == 0 || valVec.size()==0)
    {
        printf("Insert field or value is null\n");
        return false;
    }

    if(fieldNameList.size() != valVec.size())
    {
        printf("Insert fieldNameVec.size() != valVec.size()\n");
        return false;
    }

    QString fieldstr = fieldNameList[0];
    QString valstr = "?";
    for(int i=1; i > &valVec, 
	const QString &connectionName)
{
	if (fieldNameList.size() == 0 || valVec.size() == 0)
	{
		printf("Insert field or value is null\n");
		return false;
	}

	if (fieldNameList.size() != valVec[0].size())
	{
		printf("Insert fieldNameVec.size() != valVec.size()\n");
		return false;
	}

	QString fieldstr = fieldNameList[0];
	QString valstr = "?";
	for (int i = 1; i < fieldNameList.size(); i++)
	{
		fieldstr += "," + fieldNameList[i];
		valstr += QString(",") + "?";
	}
	
	QSqlDatabase db = QSqlDatabase::database(connectionName);
	QSqlQuery query(db);

	QString str = QString("insert into %1(%2) values(%3)").arg(tableName).arg(fieldstr).arg(valstr);
	printf("%s\n", str.toLocal8Bit().data());
	db.transaction();
	for (int i = 0; i < valVec.size(); i++)
	{
		query.prepare(str);
		for (int j = 0; j &conditionMap)
{
    QString conditionStr;
    QMap::const_iterator it = conditionMap.begin();
    for(; it!=conditionMap.end(); it++)
    {
        if(it != conditionMap.begin())
        {
            conditionStr += " and ";
        }
        conditionStr += it.key() + QString("= '") + it.value().toString() + QString("'");
    }
    return conditionStr;
}

bool SqlInterface::LoadPixMap(const QString &filepath, QPixmap &pix)
{
    QPixmap p(filepath);
   if(!p.isNull())
   {
       pix = p;
       return true;
   }

   return false;
}

bool SqlInterface::SavePixMap(const QString &filepath, const QPixmap &pix)
{
    return pix.save(filepath);
}


#if 0
//测试用例
const QString tablename("interface"), connName("test"),tablename1("interface1"),connName1("test1");
SqlInterface::Open("./QtTest/QtTest/test.s3db",connName);
SqlInterface::Open("./QtTest/QtTest/test1.s3db",connName1);

//查询
QVector vec;
bool succ = SqlInterface::Get(tablename,"name",vec,"age = 12",connName);
qDebug("vec.size = %d",vec.size());
for(int i = 0 ;i map;
map["age"] = 12;
vec.clear();
succ = SqlInterface::Get(tablename,"name",vec,map,connName);
qDebug("get:suc[%d] vec.size()[%d]",succ,vec.size());
for(int i = 0 ;i > vvec;
//succ = sql.Get(tablename,fnameV,vvec,"name = 'liubei'");
succ = SqlInterface::Get(tablename,fnameLs,vvec,map,connName);
qDebug("succ[%d],vec.size[%d]",succ,vvec.size());
for(int i=0; i valvv;
valvv.push_back("zhaoyun");
valvv.push_back("24");
valvv.push_back("13631421");

succ = SqlInterface::Update(tablename,fnameLs,valvv,"name = 'zhaoyun'",connName);
qDebug("Update:succ[%d]",succ);

//插入
QVector invec;
invec.push_back("zhangfei");
invec.push_back(28);
invec.push_back("1542452");
succ = SqlInterface::Insert(tablename,fnameLs,invec,connName);
qDebug("insert:succ[%d]",succ);

const QString testtable("tabletest");
//同一数据库不同表查询查询
QVector tvec;
succ = SqlInterface::Get(testtable,"name",tvec,"age = 12",connName);
for(int i=0; i

你可能感兴趣的:(qt)