首页 > 代码库 > Qt Sqlite封装类SqliteUtil

Qt Sqlite封装类SqliteUtil

在网上找了很久关于Qt访问Sqlite数据库的封装类,但是没能找到一个很好的访问调用类,自己写了一个出来,在这里分享一下,希望能对大家有所帮助,小弟不才,写代码没多少经验,如果有什么不恰当之处,请批评指出:

sqliteutil.h

#ifndef SQLITEUTIL_H#define SQLITEUTIL_H#include <QSqlDatabase>#include <QSqlQuery>#include <QList>#include <QVariant>#include <QSqlQueryModel>class SqliteUtil{public:    explicit SqliteUtil(const QString& strDatabase = "smart.db");    ~SqliteUtil();        bool createConnection(const QString& strConn);    QSqlRecord ExecuteRecord(const QString& strQuery);    QSqlRecord ExecuteRecord(const QString& strQuery, QList<QVariant> lstParameter);    QSqlRecord ExecuteRecord(const QString& strQuery, QVariant Parameter);    QSqlQuery ExecuteSqlQuery(const QString& strQuery, QList<QVariant> lstParameter);    QSqlQuery ExecuteSqlQuery(const QString& strQuery, QVariant Parameter);    QSqlQuery ExecuteSqlQuery(const QString& strQuery);    int ExecuteInt(const QString& strQuery);    int ExecuteInt(const QString& strQuery, QList<QVariant> lstParameter);    int ExecuteInt(const QString& strQuery, QVariant Parameter);    bool Execute(const QString& strQuery, QVariant Parameter);    bool Execute(const QString& strQuery, QList<QVariant> lstParameter);    QString ExecuteString(const QString& strQuery);    void ExecuteQueryModel(QSqlQueryModel *p_queryModel, const QString& strQuery);    void ExecuteQueryModel(QSqlQueryModel *p_queryModel, const QString& strQuery,                           QList<QVariant> lstParameter);    void ExecuteQueryModel(QSqlQueryModel *p_queryModel, const QString& strQuery, QVariant Parameter);private:    QSqlDatabase m_db;    QString m_strDatabase;    };#endif // SQLITEUTIL_H

sqliteutil.cpp

#include "sqliteutil.h"#include <QDebug>/******************************************************************************** Copyright (C) 2014 scutemos-huabo** All rights reserved.** Contact: wiessharling@qq.com** Please keep the author contact information.** 2014-07-16******************************************************************************/SqliteUtil::SqliteUtil(const QString &strDatabase) :    m_strDatabase(strDatabase){    createConnection(m_strDatabase);}SqliteUtil::~SqliteUtil(){    m_db.close();}bool SqliteUtil::createConnection(const QString &database){    //与数据库建立连接    if (QSqlDatabase::contains("my_conn"))    {        m_db = QSqlDatabase::database("my_conn");    }    else    {        m_db = QSqlDatabase::addDatabase("QSQLITE", "my_conn");    }    //设置数据库名    m_db.setDatabaseName(database);    //打开数据库    if (!m_db.open())    {        qDebug() << "Open database failed!";        return false;    }    else    {        return true;    }}QSqlRecord SqliteUtil::ExecuteRecord(const QString& strQuery){    if(!m_db.isOpen())    {        createConnection(m_strDatabase);    }    QSqlQuery query(m_db);    query.prepare(strQuery);    query.exec();    return query.record();}QSqlRecord SqliteUtil::ExecuteRecord(const QString& strQuery, QList<QVariant> lstParameter){    if(!m_db.isOpen())    {        createConnection(m_strDatabase);    }    QSqlQuery query(m_db);    query.prepare(strQuery);    for(int i = 0; i < lstParameter.count(); i++)        query.bindValue(i, lstParameter[i]);    query.exec();    return query.record();}QSqlRecord SqliteUtil::ExecuteRecord(const QString& strQuery, QVariant Parameter){    if(!m_db.isOpen())    {        createConnection(m_strDatabase);    }    QSqlQuery query(m_db);    query.prepare(strQuery);    query.bindValue(0, Parameter);    query.exec();    return query.record();}QSqlQuery SqliteUtil::ExecuteSqlQuery(const QString& strQuery, QList<QVariant> lstParameter){    if(!m_db.isOpen())    {        createConnection(m_strDatabase);    }    QSqlQuery query(m_db);    query.prepare(strQuery);    query.setForwardOnly(true);    for(int i = 0; i < lstParameter.count(); i++)        query.bindValue(i, lstParameter[i]);    query.exec();    return query;}QSqlQuery SqliteUtil::ExecuteSqlQuery(const QString& strQuery, QVariant Parameter){    if(!m_db.isOpen())    {        createConnection(m_strDatabase);    }    QSqlQuery query(m_db);    query.setForwardOnly(true);    query.prepare(strQuery);    query.bindValue(0, Parameter);    query.exec();    return query;}QSqlQuery SqliteUtil::ExecuteSqlQuery(const QString& strQuery){    if(!m_db.isOpen())    {        createConnection(m_strDatabase);    }    QSqlQuery query(m_db);    query.setForwardOnly(true);    query.prepare(strQuery);    query.exec();    return query;}int SqliteUtil::ExecuteInt(const QString& strQuery){    if(!m_db.isOpen())    {        createConnection(m_strDatabase);    }    QSqlQuery query(m_db);    query.prepare(strQuery);    query.exec();    int ID = 0;    while(query.next())    {        ID = query.value(0).toInt();    }    return ID;}int SqliteUtil::ExecuteInt(const QString& strQuery, QList<QVariant> lstParameter){    if(!m_db.isOpen())    {        createConnection(m_strDatabase);    }    QSqlQuery query(m_db);    query.prepare(strQuery);    for(int i = 0; i < lstParameter.count(); i++)        query.bindValue(i, lstParameter[i]);    query.exec();    int ID = 0;    while(query.next())    {        ID = query.value(0).toInt();    }    return ID;}int SqliteUtil::ExecuteInt(const QString& strQuery, QVariant Parameter){    if(!m_db.isOpen())    {        createConnection(m_strDatabase);    }    QSqlQuery query(m_db);    query.prepare(strQuery);    query.bindValue(0, Parameter);    query.exec();    int ID = 0;    while(query.next())    {        ID = query.value(0).toInt();    }    return ID;}bool SqliteUtil::Execute(const QString& strQuery, QVariant Parameter){    if(!m_db.isOpen())    {        createConnection(m_strDatabase);    }    QSqlQuery query(m_db);    query.prepare(strQuery);    query.bindValue(0, Parameter);    return query.exec();}bool SqliteUtil::Execute(const QString& strQuery, QList<QVariant> lstParameter){    if(!m_db.isOpen())    {        createConnection(m_strDatabase);    }    QSqlQuery query(m_db);    query.prepare(strQuery);    for(int i = 0; i < lstParameter.count(); i++)        query.bindValue(i, lstParameter[i]);    return query.exec();}QString SqliteUtil::ExecuteString(const QString& strQuery){    if(!m_db.isOpen())    {        createConnection(m_strDatabase);    }    QSqlQuery query(m_db);    query.prepare(strQuery);    query.exec();    QString temp;    while(query.next())    {        temp = query.value(0).toString();    }    return temp;}void SqliteUtil::ExecuteQueryModel(QSqlQueryModel *p_queryModel, const QString& strQuery){    if(!m_db.isOpen())    {        createConnection(m_strDatabase);    }    p_queryModel->setQuery(strQuery, m_db);}void SqliteUtil::ExecuteQueryModel(QSqlQueryModel *p_queryModel, const QString& strQuery,                       QList<QVariant> lstParameter){    if(!m_db.isOpen())    {        createConnection(m_strDatabase);    }    QSqlQuery query(m_db);    query.prepare(strQuery);    for(int i = 0; i < lstParameter.count(); i++)        query.bindValue(i, lstParameter[i]);    p_queryModel->setQuery(query);}void SqliteUtil::ExecuteQueryModel(QSqlQueryModel *p_queryModel, const QString& strQuery,                                   QVariant Parameter){    if(!m_db.isOpen())    {        createConnection(m_strDatabase);    }    QSqlQuery query(m_db);    query.prepare(strQuery);    query.bindValue(0, Parameter);    p_queryModel->setQuery(query); }