Query and executeNoQuery method for SQLHelper

This commit is contained in:
Krad
2021-11-11 14:08:44 +08:00
parent 9d61be2445
commit 2c83a26f06
2 changed files with 26 additions and 0 deletions

View File

@@ -57,3 +57,27 @@ int SQLHelper::QueryCount(QString sql) {
}
return 0;
}
int SQLHelper::ExecuteNoQuery(QString sql) {
QSqlQuery query(*defaultDatabase);
qDebug()<<sql;
if (query.exec(sql))
{
return query.numRowsAffected();
}
return 0;
}
void SQLHelper::Query(QString sql,QMap<QString,QVariant>& result) {
QSqlQuery query(*defaultDatabase);
if (query.exec(sql))
{
if(query.next())
{
for(int i=0; i<query.record().count(); i++)
{
result[query.record().fieldName(i)]=query.record().value(i);
}
}
}
}

View File

@@ -16,7 +16,9 @@ public:
static bool Open();
static bool Open(QSqlDatabase* base);
static void Close();
static void Query(QString sql,QMap<QString,QVariant>& result);
static int QueryCount(QString sql);
static int ExecuteNoQuery(QString sql);
static QSqlTableModel* getTable(const QString & tableName);
private:
static QSqlDatabase* defaultDatabase;