C++ 单例模式

有时候用到单例模式,记录一下。

另外,有时候也用 静态函数。

使用时,直接调用即可:

DatabaseManager::instance()->InsertSqlData("table", listField, listVal);

 

头文件:

有时候可能需要引用 include<QSharedPointer>

 1 #ifndef DATABASEMANAGER_H
 2 #define DATABASEMANAGER_H
 3 
 4 
 5 #include "operatesqlite.h"
 6 #include <QMutex>
 7 #include <QMutexLocker>
 8 
 9 class DatabaseManager
10 {
11 public:
12     static QSharedPointer<DatabaseManager>& instance()
13     {
14 
15         if (m_pInstance.isNull())
16         {
17             QMutexLocker mutexLocker(&m_Mutex);
18             if (m_pInstance.isNull())
19                 m_pInstance = QSharedPointer<DatabaseManager>(new DatabaseManager());
20         }
21         return m_pInstance;
22     }
23 private:
24     DatabaseManager();
25     DatabaseManager(const DatabaseManager&){}
26     DatabaseManager& operator==(const DatabaseManager&){}
27 private:
28     static QMutex m_Mutex;
29     static QSharedPointer<DatabaseManager> m_pInstance;
30 
31 
32 public:
33     //向表中插入数据
34     bool InsertSqlData(QString tbName, QStringList& listField, QStringList& listVal);
35     //记录是否存在
36     bool IsRecordExist(const QString& tbName, const QString& field, const QString& value);
37     bool GetRecordData(QString strSql);
38     bool GetAirportInfo(QTableWidget* tbAirportInfo);
39     bool DeleteRecord(const QString& tbName, const QString& field, const QString& fieldVal);
40 
41 private:
42     OperateSqlite   *m_pOperateSqlite;
43 };
44 
45 
46 
47 #endif // DATABASEMANAGER_H

cpp文件:

 1 #include "databasemanager.h"
 2 #include <QSqlRecord>
 3 #include <QDebug>
 4 
 5 QMutex DatabaseManager::m_Mutex;
 6 QSharedPointer<DatabaseManager> DatabaseManager::m_pInstance = nullptr;
 7 
 8 DatabaseManager::DatabaseManager()
 9 {
10     m_pOperateSqlite = new OperateSqlite;
11 }
12 
13 bool DatabaseManager::InsertSqlData(QString tbName, QStringList &listField, QStringList &listVal)
14 {
15      return m_pOperateSqlite->InserSqlData(tbName, listField, listVal);
16 }
17 
18 bool DatabaseManager::IsRecordExist(const QString &tbName, const QString &field, const QString &value)
19 {
20     return m_pOperateSqlite->IsRecordExist1(tbName, field, value);
21 }
22 
23 bool DatabaseManager::GetRecordData(QString strSql)
24 {
25     QSqlQuery q(m_pOperateSqlite->GetSqlDataBase());
26     //QString sql = "select * from tbAirportInfo";
27     if(m_pOperateSqlite->GetRecordData(q, strSql)){
28         int nNameIndex = q.record().indexOf("name");
29         int nLongitudeIndex = q.record().indexOf("longitude");
30         while (q.next()) {
31             QString strName = q.value(nNameIndex).toString();
32             QString strLongitude = q.value(nLongitudeIndex).toString();
33             qDebug() << strName;
34             qDebug() <<strLongitude;
35         }
36         return true;
37     }
38     return false;
39 }
40 
41 bool DatabaseManager::GetAirportInfo(QTableWidget *tbAirportInfo)
42 {
43     QSqlQuery q(m_pOperateSqlite->GetSqlDataBase());
44     QString sql = "select * from tbAirportInfo";
45     if(m_pOperateSqlite->GetRecordData(q, sql)){
46         int nNameIndex = q.record().indexOf("name");
47         int nLongitudeIndex = q.record().indexOf("longitude");
48         int nLatitudeIndex = q.record().indexOf("latitude");
49         while (q.next()) {
50             QString strName = q.value(nNameIndex).toString();
51             QString strLongitude = q.value(nLongitudeIndex).toString();
52             QString strLatitude = q.value(nLatitudeIndex).toString();
53             QStringList list;
54             list << strName << strLongitude << strLatitude;
55 
56             int nRowCount = tbAirportInfo->rowCount();
57             tbAirportInfo->setRowCount(nRowCount + 1);
58 
59             tbAirportInfo->setRowHeight(nRowCount, 60);
60             QTableWidgetItem* item = new QTableWidgetItem(QString("%1").arg(nRowCount + 1));
61             item->setTextAlignment(Qt::AlignCenter);
62             tbAirportInfo->setItem(nRowCount, 0, item);
63             for(int i = 0; i < list.size(); i++)
64             {
65                 item = new QTableWidgetItem(list.at(i));
66                 item->setTextAlignment(Qt::AlignCenter);
67                 tbAirportInfo->setItem(nRowCount, i + 1, item);
68             }
69         }
70         return true;
71     }
72     return false;
73 }
74 
75 bool DatabaseManager::DeleteRecord(const QString &tbName, const QString &field, const QString& fieldVal)
76 {
77     QSqlQuery q(m_pOperateSqlite->GetSqlDataBase());
78     QString sql = QString("delete from %1 where %2 = '%3'").arg(tbName).arg(field).arg(fieldVal);
79     return m_pOperateSqlite->ExecSql(sql);
80 }

 

posted @ 2022-11-10 10:43  阳光下的小土豆  阅读(24)  评论(0编辑  收藏  举报