添加项目文件。
This commit is contained in:
@@ -0,0 +1,439 @@
|
||||
#include <QDebug>
|
||||
#include "sqliteutil.h"
|
||||
#include <QSqlDatabase>
|
||||
#include <QSqlQuery>
|
||||
#include <QList>
|
||||
#include <QVariant>
|
||||
#include <QSqlQueryModel>
|
||||
#include <QSqlRecord>
|
||||
#include <QSqlError>
|
||||
#include <QApplication>
|
||||
#include <QDir>
|
||||
QSqlDatabase SqliteUtil::m_db ;
|
||||
|
||||
QString SqliteUtil::m_strDatabase = "SQL/1208_02_jixiebi.db";
|
||||
|
||||
SqliteUtil::SqliteUtil()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
bool SqliteUtil::createConnection(const QString &database)
|
||||
{
|
||||
//与数据库建立连接
|
||||
if (QSqlDatabase::contains("qt_sql_default_connection"))
|
||||
{
|
||||
m_db = QSqlDatabase::database("qt_sql_default_connection");
|
||||
}
|
||||
else
|
||||
{
|
||||
// 修复:使用一致的连接名称
|
||||
m_db = QSqlDatabase::addDatabase("QSQLITE", "qt_sql_default_connection");
|
||||
}
|
||||
//设置数据库名
|
||||
m_db.setDatabaseName(database);
|
||||
//打开数据库
|
||||
if (!m_db.open())
|
||||
{
|
||||
qDebug() << "打开数据库失败!" << m_db.lastError().text();
|
||||
return false;
|
||||
}
|
||||
qDebug() << "数据库连接成功:" << database;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SqliteUtil::ExecuteRecord(const QString& strQuery)
|
||||
{
|
||||
if(!m_db.isOpen())
|
||||
{
|
||||
if(!createConnection(m_strDatabase))
|
||||
{
|
||||
qDebug() << "在ExecuteRecord中创建数据库连接失败!" << m_db.lastError().text();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
QSqlQuery query(m_db);
|
||||
query.prepare(strQuery);
|
||||
if(query.exec()){
|
||||
return true;
|
||||
}
|
||||
qDebug() << "查询执行失败!" << query.lastError().text() << "SQL:" << strQuery;
|
||||
return false;
|
||||
}
|
||||
|
||||
QSqlRecord SqliteUtil::ExecuteRecord(const QString& strQuery, QList<QVariant> lstParameter)
|
||||
{
|
||||
if(!m_db.isOpen())
|
||||
{
|
||||
if(!createConnection(m_strDatabase))
|
||||
{
|
||||
qDebug() << "在ExecuteRecord中创建数据库连接失败!" << m_db.lastError().text();
|
||||
return QSqlRecord();
|
||||
}
|
||||
}
|
||||
QSqlQuery query(m_db);
|
||||
query.prepare(strQuery);
|
||||
for(int i = 0; i < lstParameter.size(); i++)
|
||||
{
|
||||
query.bindValue(i, lstParameter.at(i));
|
||||
}
|
||||
if(query.exec())
|
||||
{
|
||||
if(query.next())
|
||||
{
|
||||
return query.record();
|
||||
}
|
||||
qDebug() << "查询执行失败!未找到记录!" << strQuery;
|
||||
return QSqlRecord();
|
||||
}
|
||||
qDebug() << "查询执行失败!" << query.lastError().text() << "SQL:" << strQuery;
|
||||
return QSqlRecord();
|
||||
}
|
||||
|
||||
QSqlRecord SqliteUtil::ExecuteRecord(const QString& strQuery, QVariant Parameter)
|
||||
{
|
||||
if(!m_db.isOpen())
|
||||
{
|
||||
if(!createConnection(m_strDatabase))
|
||||
{
|
||||
qDebug() << "在ExecuteRecord中创建数据库连接失败!" << m_db.lastError().text();
|
||||
return QSqlRecord();
|
||||
}
|
||||
}
|
||||
QSqlQuery query(m_db);
|
||||
query.prepare(strQuery);
|
||||
query.bindValue(0, Parameter);
|
||||
if(query.exec())
|
||||
{
|
||||
if(query.next())
|
||||
{
|
||||
return query.record();
|
||||
}
|
||||
qDebug() << "查询执行失败!未找到记录!" << strQuery;
|
||||
return QSqlRecord();
|
||||
}
|
||||
qDebug() << "查询执行失败!" << query.lastError().text() << "SQL:" << strQuery;
|
||||
return QSqlRecord();
|
||||
}
|
||||
|
||||
QSqlQuery SqliteUtil::ExecuteSqlQuery(const QString& strQuery, QList<QVariant> lstParameter)
|
||||
{
|
||||
if(!m_db.isOpen())
|
||||
{
|
||||
if(!createConnection(m_strDatabase))
|
||||
{
|
||||
qDebug() << "在ExecuteSqlQuery中创建数据库连接失败!" << m_db.lastError().text();
|
||||
return QSqlQuery();
|
||||
}
|
||||
}
|
||||
QSqlQuery query(m_db);
|
||||
query.prepare(strQuery);
|
||||
query.setForwardOnly(true);
|
||||
for(int i = 0; i < lstParameter.size(); i++)
|
||||
{
|
||||
query.bindValue(i, lstParameter.at(i));
|
||||
}
|
||||
if(!query.exec())
|
||||
{
|
||||
qDebug() << "查询执行失败!" << query.lastError().text() << "SQL:" << strQuery;
|
||||
}
|
||||
return query;
|
||||
}
|
||||
|
||||
QSqlQuery SqliteUtil::ExecuteSqlQuery(const QString& strQuery, QVariant Parameter)
|
||||
{
|
||||
if(!m_db.isOpen())
|
||||
{
|
||||
if(!createConnection(m_strDatabase))
|
||||
{
|
||||
qDebug() << "在ExecuteSqlQuery中创建数据库连接失败!" << m_db.lastError().text();
|
||||
return QSqlQuery();
|
||||
}
|
||||
}
|
||||
QSqlQuery query(m_db);
|
||||
query.setForwardOnly(true);
|
||||
query.prepare(strQuery);
|
||||
query.bindValue(0, Parameter);
|
||||
if(!query.exec())
|
||||
{
|
||||
qDebug() << "查询执行失败!" << query.lastError().text() << "SQL:" << strQuery;
|
||||
}
|
||||
return query;
|
||||
}
|
||||
|
||||
QSqlQuery SqliteUtil::ExecuteSqlQuery(const QString& strQuery)
|
||||
{
|
||||
if(!m_db.isOpen())
|
||||
{
|
||||
if(!createConnection(m_strDatabase))
|
||||
{
|
||||
qDebug() << "在ExecuteSqlQuery中创建数据库连接失败!" << m_db.lastError().text();
|
||||
return QSqlQuery();
|
||||
}
|
||||
}
|
||||
QSqlQuery query(m_db);
|
||||
query.prepare(strQuery);
|
||||
query.setForwardOnly(true);
|
||||
if(!query.exec())
|
||||
{
|
||||
qDebug() << "查询执行失败!" << query.lastError().text() << "SQL:" << strQuery;
|
||||
}
|
||||
return query;
|
||||
}
|
||||
|
||||
int SqliteUtil::ExecuteInt(const QString& strQuery)
|
||||
{
|
||||
if(!m_db.isOpen())
|
||||
{
|
||||
if(!createConnection(m_strDatabase))
|
||||
{
|
||||
qDebug() << "在ExecuteInt中创建数据库连接失败!" << m_db.lastError().text();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
QSqlQuery query(m_db);
|
||||
query.prepare(strQuery);
|
||||
if(query.exec())
|
||||
{
|
||||
if(query.next())
|
||||
{
|
||||
return query.value(0).toInt();
|
||||
}
|
||||
qDebug() << "查询执行失败!未找到记录!" << strQuery;
|
||||
return -1;
|
||||
}
|
||||
qDebug() << "查询执行失败!" << query.lastError().text() << "SQL:" << strQuery;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int SqliteUtil::ExecuteInt(const QString& strQuery, QList<QVariant> lstParameter)
|
||||
{
|
||||
if(!m_db.isOpen())
|
||||
{
|
||||
if(!createConnection(m_strDatabase))
|
||||
{
|
||||
qDebug() << "在ExecuteInt中创建数据库连接失败!" << m_db.lastError().text();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
QSqlQuery query(m_db);
|
||||
query.prepare(strQuery);
|
||||
for(int i = 0; i < lstParameter.size(); i++)
|
||||
{
|
||||
query.bindValue(i, lstParameter.at(i));
|
||||
}
|
||||
if(query.exec())
|
||||
{
|
||||
if(query.next())
|
||||
{
|
||||
return query.value(0).toInt();
|
||||
}
|
||||
qDebug() << "查询执行失败!未找到记录!" << strQuery;
|
||||
return -1;
|
||||
}
|
||||
qDebug() << "查询执行失败!" << query.lastError().text() << "SQL:" << strQuery;
|
||||
return -1;
|
||||
}
|
||||
|
||||
int SqliteUtil::ExecuteInt(const QString& strQuery, QVariant Parameter)
|
||||
{
|
||||
if(!m_db.isOpen())
|
||||
{
|
||||
if(!createConnection(m_strDatabase))
|
||||
{
|
||||
qDebug() << "在ExecuteInt中创建数据库连接失败!" << m_db.lastError().text();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
QSqlQuery query(m_db);
|
||||
query.prepare(strQuery);
|
||||
query.bindValue(0, Parameter);
|
||||
if(query.exec())
|
||||
{
|
||||
if(query.next())
|
||||
{
|
||||
return query.value(0).toInt();
|
||||
}
|
||||
qDebug() << "查询执行失败!未找到记录!" << strQuery;
|
||||
return -1;
|
||||
}
|
||||
qDebug() << "查询执行失败!" << query.lastError().text() << "SQL:" << strQuery;
|
||||
return -1;
|
||||
}
|
||||
|
||||
bool SqliteUtil::Execute(const QString& strQuery)
|
||||
{
|
||||
if(!m_db.isOpen())
|
||||
{
|
||||
if(!createConnection(m_strDatabase))
|
||||
{
|
||||
qDebug() << "Failed to create database connection in Execute";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
QSqlQuery query(m_db);
|
||||
if(query.exec(strQuery))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
qDebug() << "查询执行失败!" << query.lastError().text() << "SQL:" << strQuery;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool SqliteUtil::Execute(const QString& strQuery, QVariant Parameter)
|
||||
{
|
||||
if(!m_db.isOpen())
|
||||
{
|
||||
if(!createConnection(m_strDatabase))
|
||||
{
|
||||
qDebug() << "在Execute中创建数据库连接失败!" << m_db.lastError().text();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
QSqlQuery query(m_db);
|
||||
query.prepare(strQuery);
|
||||
query.bindValue(0, Parameter);
|
||||
if(query.exec())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
qDebug() << "查询执行失败!" << query.lastError().text() << "SQL:" << strQuery;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool SqliteUtil::Execute(const QString& strQuery, QList<QVariant> lstParameter)
|
||||
{
|
||||
if(!m_db.isOpen())
|
||||
{
|
||||
if(!createConnection(m_strDatabase))
|
||||
{
|
||||
qDebug() << "在Execute中创建数据库连接失败!" << m_db.lastError().text();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
QSqlQuery query(m_db);
|
||||
query.prepare(strQuery);
|
||||
for(int i = 0; i < lstParameter.size(); i++)
|
||||
{
|
||||
query.bindValue(i, lstParameter.at(i));
|
||||
}
|
||||
if(query.exec())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
qDebug() << "查询执行失败!" << query.lastError().text() << "SQL:" << strQuery;
|
||||
return false;
|
||||
}
|
||||
|
||||
QString SqliteUtil::ExecuteString(const QString& strQuery)
|
||||
{
|
||||
if(!m_db.isOpen())
|
||||
{
|
||||
if(!createConnection(m_strDatabase))
|
||||
{
|
||||
qDebug() << "在ExecuteString中创建数据库连接失败!" << m_db.lastError().text();
|
||||
return QString();
|
||||
}
|
||||
}
|
||||
QSqlQuery query(m_db);
|
||||
query.prepare(strQuery);
|
||||
if(query.exec())
|
||||
{
|
||||
if(query.next())
|
||||
{
|
||||
return query.value(0).toString();
|
||||
}
|
||||
qDebug() << "查询执行失败!未找到记录!" << strQuery;
|
||||
return QString();
|
||||
}
|
||||
qDebug() << "查询执行失败!" << query.lastError().text() << "SQL:" << strQuery;
|
||||
return QString();
|
||||
}
|
||||
|
||||
void SqliteUtil::ExecuteQueryModel(QSqlQueryModel *p_queryModel, const QString& strQuery)
|
||||
{
|
||||
if(!m_db.isOpen())
|
||||
{
|
||||
if(!createConnection(m_strDatabase))
|
||||
{
|
||||
qDebug() << "在ExecuteQueryModel中创建数据库连接失败!" << m_db.lastError().text();
|
||||
return;
|
||||
}
|
||||
}
|
||||
if(p_queryModel == nullptr)
|
||||
{
|
||||
qDebug() << "在ExecuteQueryModel中查询模型指针为空!" << strQuery;
|
||||
return;
|
||||
}
|
||||
p_queryModel->setQuery(strQuery, m_db);
|
||||
if(p_queryModel->lastError().isValid())
|
||||
{
|
||||
qDebug() << "在ExecuteQueryModel中查询模型执行失败!" << p_queryModel->lastError().text() << "SQL:" << strQuery;
|
||||
}
|
||||
}
|
||||
|
||||
void SqliteUtil::ExecuteQueryModel(QSqlQueryModel *p_queryModel, const QString& strQuery,
|
||||
QList<QVariant> lstParameter)
|
||||
{
|
||||
if(!m_db.isOpen())
|
||||
{
|
||||
if(!createConnection(m_strDatabase))
|
||||
{
|
||||
qDebug() << "在ExecuteQueryModel中创建数据库连接失败!" << m_db.lastError().text();
|
||||
return;
|
||||
}
|
||||
}
|
||||
if(p_queryModel == nullptr)
|
||||
{
|
||||
qDebug() << "在ExecuteQueryModel中查询模型指针为空!" << strQuery;
|
||||
return;
|
||||
}
|
||||
QSqlQuery query(m_db);
|
||||
query.prepare(strQuery);
|
||||
for(int i = 0; i < lstParameter.size(); i++)
|
||||
{
|
||||
query.bindValue(i, lstParameter.at(i));
|
||||
}
|
||||
if(query.exec())
|
||||
{
|
||||
p_queryModel->setQuery(query);
|
||||
if(p_queryModel->lastError().isValid())
|
||||
{
|
||||
qDebug() << "在ExecuteQueryModel中查询模型执行失败!" << p_queryModel->lastError().text() << "SQL:" << strQuery;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
qDebug() << "在ExecuteQueryModel中查询模型执行失败!" << query.lastError().text() << "SQL:" << strQuery;
|
||||
}
|
||||
}
|
||||
|
||||
void SqliteUtil::ExecuteQueryModel(QSqlQueryModel *p_queryModel, const QString& strQuery, QVariant Parameter)
|
||||
{
|
||||
if(!m_db.isOpen())
|
||||
{
|
||||
if(!createConnection(m_strDatabase))
|
||||
{
|
||||
qDebug() << "在ExecuteQueryModel中创建数据库连接失败!" << m_db.lastError().text();
|
||||
return;
|
||||
}
|
||||
}
|
||||
if(p_queryModel == nullptr)
|
||||
{
|
||||
qDebug() << "在ExecuteQueryModel中查询模型指针为空!" << strQuery;
|
||||
return;
|
||||
}
|
||||
QSqlQuery query(m_db);
|
||||
query.prepare(strQuery);
|
||||
query.bindValue(0, Parameter);
|
||||
if(query.exec())
|
||||
{
|
||||
p_queryModel->setQuery(query);
|
||||
if(p_queryModel->lastError().isValid())
|
||||
{
|
||||
qDebug() << "在ExecuteQueryModel中查询模型执行失败!" << p_queryModel->lastError().text() << "SQL:" << strQuery;
|
||||
}
|
||||
} else {
|
||||
qDebug() << "在ExecuteQueryModel中查询模型执行失败!" << query.lastError().text() << "SQL:" << strQuery;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user