QT讀寫(xiě)Sqlite數(shù)據(jù)庫(kù)的三種方式
QT對(duì)一些基本的數(shù)據(jù)庫(kù)的訪問(wèn)封裝,可謂是極大的方便的我們開(kāi)發(fā)人員,現(xiàn)在我們就來(lái)說(shuō)下QT對(duì)Sqlite這個(gè)數(shù)據(jù)庫(kù)的讀寫(xiě),Sqlite是一個(gè)比較小型的本地?cái)?shù)據(jù)庫(kù),對(duì)于保存一些軟件配置參數(shù)或量不是很大的數(shù)據(jù)是相當(dāng)?shù)姆奖悖琎t本身已經(jīng)自帶了Sqlite的驅(qū)動(dòng),直接使用相關(guān)的類(lèi)庫(kù)即可,這篇我們主要來(lái)說(shuō)明QT訪問(wèn)Sqlite數(shù)據(jù)庫(kù)的三種方式(即使用三種類(lèi)庫(kù)去訪問(wèn)),分別為QSqlQuery、QSqlQueryModel、QSqlTableModel,對(duì)于這三種類(lèi)庫(kù),可看為一個(gè)比一個(gè)上層,也就是封裝的更厲害,甚至第三種QSqlTableModel,根本就不需要開(kāi)發(fā)者懂SQL語(yǔ)言,也能操作Sqlite數(shù)據(jù)庫(kù)。
1、首先使用QSqlQuery來(lái)訪問(wèn)
我們先要在工程中包含與數(shù)據(jù)庫(kù)相關(guān)的幾個(gè)頭文件#include <QtSql/QSqlDatabase> 、#include <QtSql/QSqlRecord>、#include <QtSql/QSqlQuery>
訪問(wèn)的數(shù)據(jù)庫(kù)內(nèi)容結(jié)構(gòu)為:
#include <QtWidgets/QApplication> #include <QCoreApplication> #include <QDebug> #include <QtSql/QSqlDatabase> #include <QtSql/QSqlQuery> #include <QtSql/QSqlRecord> typedef struct _testInfo //假定數(shù)據(jù)庫(kù)存儲(chǔ)內(nèi)容 { QString UsreName; QString IP; QString Port; QString PassWord; QString Type; }testInfo; int main(int argc, char *argv[]) { QApplication a(argc, argv); QVector<testInfo> infoVect; //testInfo向量,用于存儲(chǔ)數(shù)據(jù)庫(kù)查詢到的數(shù)據(jù) QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName(QApplication::applicationDirPath() + "/CONFIG/" + "CONFIG.db"); if (!db.open()) { return 0; } /**************************使用QSqlQuery操作數(shù)據(jù)庫(kù)**************************/ QSqlQuery query; //執(zhí)行操作類(lèi)對(duì)象 //查詢數(shù)據(jù) query.prepare("SELECT * FROM T_USER_MANAGE"); query.exec(); //執(zhí)行 QSqlRecord recode = query.record(); //recode保存查詢到一些內(nèi)容信息,如表頭、列數(shù)等等 int column = recode.count(); //獲取讀取結(jié)果的列數(shù) QString s1 = recode.fieldName(0); //獲取第0列的列名 while (query.next()) { testInfo tmp; tmp.UsreName = query.value("UsreName").toString(); tmp.IP = query.value("IP").toString(); tmp.Port = query.value("Port").toString(); tmp.PassWord = query.value("PassWord").toString(); tmp.Type = query.value("Type").toString(); infoVect.push_back(tmp); //將查詢到的內(nèi)容存到testInfo向量中 } for (int i=0; i<infoVect.size(); i++) //打印輸出 { qDebug() << infoVect[i].UsreName << ":" \ << infoVect[i].IP << ":" \ << infoVect[i].Port << ":" \ << infoVect[i].PassWord << ":" \ << infoVect[i].Type; } //插入數(shù)據(jù) query.prepare("INSERT INTO T_USER_MANAGE (UsreName, IP, Port, PassWord, Type) VALUES (:UsreName, :IP, :Port, :PassWord, :Type)"); query.bindValue(":UserName", "user4"); //給每個(gè)插入值標(biāo)識(shí)符設(shè)定具體值 query.bindValue(":IP", "192.168.1.5"); query.bindValue(":Port", "5004"); query.bindValue(":PassWord", "55555"); query.bindValue(":Type", "operator"); query.exec(); //更改表中 UserName=user4 的Type屬性為admin query.prepare("UPDATE T_USER_MANAGE SET Type='admin' WHERE UserName='user4'"); query.exec(); //刪除表中 UserName=user4的用戶信息 query.prepare("DELETE FROM T_USER_MANAGE WHERE UserName='user4'"); query.exec(); #endif /**************************使用QSqlQuery操作數(shù)據(jù)庫(kù)END***********************/
編譯輸出:
2、使用QSqlQueryModel來(lái)訪問(wèn)
QSqlQueryModel類(lèi)帶有Model字樣,相信你已經(jīng)猜到我們可以用他來(lái)關(guān)聯(lián)試圖,就能把數(shù)據(jù)庫(kù)的內(nèi)容顯示到視圖上,當(dāng)然,常規(guī)的操作也是可以的,但是我們只說(shuō)說(shuō)怎么用這個(gè)類(lèi)來(lái)把數(shù)據(jù)庫(kù)中的內(nèi)容顯示到是視圖中,這里我們選擇的視圖類(lèi)為QTableView,直接上代碼吧
#include <QtWidgets/QApplication> #include <QCoreApplication> #include <QDebug> #include <QString> #include <QTableView> #include <QtSql/QSqlDatabase> #include <QtSql/QSqlQueryModel> int main(int argc, char *argv[]) { QApplication a(argc, argv); QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName(QApplication::applicationDirPath() + "/CONFIG/" + "CONFIG.db"); if (!db.open()) { return 0; } QSqlQueryModel *model = new QSqlQueryModel; model->setQuery("SELECT * FROM T_USER_MANAGE", db); //從給定的數(shù)據(jù)庫(kù)db執(zhí)行sql操作, db需預(yù)先制定并打開(kāi) int column = model->columnCount(); //獲取列數(shù) int row = model->rowCount(); //獲取行數(shù) model->setHeaderData(0, Qt::Horizontal, QStringLiteral("用戶名")); //設(shè)置表頭,如不設(shè)置則使用數(shù)據(jù)庫(kù)中的默認(rèn)表頭 model->setHeaderData(1, Qt::Horizontal, QStringLiteral("IP地址")); model->setHeaderData(2, Qt::Horizontal, QStringLiteral("端口")); model->setHeaderData(3, Qt::Horizontal, QStringLiteral("密碼")); model->setHeaderData(4, Qt::Horizontal, QStringLiteral("用戶類(lèi)型")); QTableView *view = new QTableView; //定義視圖,只能用于顯示,不能修改數(shù)據(jù)庫(kù) view->setFixedSize(500, 200); view->setModel(model); view->show(); return a.exec(); }
編譯運(yùn)行一下:
3、最后使用QSqlTableModel來(lái)訪問(wèn)
最后我們來(lái)說(shuō)說(shuō)使用QSqlTableModel這個(gè)類(lèi)去操作Sqlite數(shù)據(jù)庫(kù),這個(gè)類(lèi)比上兩個(gè)封裝的更徹底,即使我們不懂SQL語(yǔ)言,也能實(shí)現(xiàn)對(duì)Sqlite數(shù)據(jù)庫(kù)的操作,并且這個(gè)類(lèi)也可以通過(guò)視圖來(lái)顯示修改數(shù)據(jù)庫(kù)內(nèi)容,這里我就拿這個(gè)類(lèi)做了個(gè)用戶管理模塊,其實(shí)也可以通用與其他任何一個(gè)模塊,只要在生成對(duì)象時(shí)傳入sqlite的數(shù)據(jù)庫(kù)名及要操作的表名即可。
在這個(gè)例子中,我實(shí)現(xiàn)了一個(gè)KSDemoDlg類(lèi),其實(shí)是一個(gè)對(duì)話框類(lèi),里邊包含了sqlite數(shù)據(jù)庫(kù)的顯示、修改等等功能,首先來(lái)看下效果(常規(guī)的增刪改查功能都有):
當(dāng)我們點(diǎn)擊增加、修改時(shí),右邊的編輯框便為可編輯狀態(tài)(說(shuō)明下,右邊的編輯框是隨著加載的數(shù)據(jù)庫(kù)表變化而變化的,簡(jiǎn)而言之就是可以不做修改就能操作別的Sqlite數(shù)據(jù)庫(kù)),完畢確定后便寫(xiě)進(jìn)數(shù)據(jù)庫(kù),同時(shí)也在左邊的表格中顯示
頭文件:
#ifndef __KSDEMODLG_H__ #define __KSDEMODLG_H__ #include <QDialog> #include <QPushButton> #include <QLineEdit> #include <QLabel> #include <QComboBox> #include <QGroupBox> #include <QTableView> #include <QtSql/QSqlTableModel> #include <QtSql/QSqlDatabase> class KSDemoDlg : public QDialog { Q_OBJECT enum {UPDATE, INSERT}; int m_operator; public: explicit KSDemoDlg(QString databaseName, QString dataTableName, QWidget *parent = 0 ); ~KSDemoDlg(); private: void UiInit(); protected slots: void onNewButtonClicked(); void onQueryButtonClicked(); void onUpdateButtonClicked(); void onDeleteButtonClicked(); void onPrimaryKeyLineEditEmpty(const QString & text); void onCurrentTableViewClicked(const QModelIndex & index); void onOKButtonClicked(); void onCancelButtonClicked(); private: QSqlDatabase m_db; QString m_DBName; QString m_DBTableName; private: QTableView* m_TabView; QSqlTableModel* m_model; private: QList<QLineEdit*> m_infoEditList; QList<QLabel*> m_infoLabelList; QPushButton m_OKButton; QPushButton m_CancelButton; private: /*所有用戶信息容器組*/ QGroupBox m_Group; QLabel m_PrimaryKeyLabel; QLineEdit m_PrimaryKeyLineEdit; QPushButton m_QueryButton; QPushButton m_NewButton; QPushButton m_UpdateButton; QPushButton m_DeleteButton; /*所選擇用戶信息容器組*/ QGroupBox m_SelectGroup; }; #endif // __KSDEMODLG_H__
.cpp文件
#include <QtWidgets/QApplication> #include <QCoreApplication> #include <QString> #include <QFormLayout> #include <QVBoxLayout> #include <QHBoxLayout> #include <QMessageBox> #include <QtSql/QSqlRecord> #include <QDebug> #include "KSDemoDlg.h" /************************************************************************** * 函數(shù)名稱:KSDemoDlg * 函數(shù)功能:用戶管理對(duì)話框構(gòu)造函數(shù) * 輸入?yún)?shù):無(wú) * 輸出參數(shù):無(wú) * 返回?cái)?shù)值:void * 創(chuàng)建人員: * 創(chuàng)建時(shí)間:2017-11-15 * 修改人員: * 修改時(shí)間: **************************************************************************/ KSDemoDlg::KSDemoDlg(QString databaseName, QString dataTableName, QWidget *parent):QDialog(parent, Qt::WindowCloseButtonHint | Qt::WindowMinMaxButtonsHint | Qt::WindowStaysOnTopHint), m_Group(this), m_PrimaryKeyLabel(this), m_PrimaryKeyLineEdit(this), m_QueryButton(this), m_NewButton(this), m_UpdateButton(this), m_DeleteButton(this), m_TabView(NULL),m_model(NULL), m_OKButton(this),m_CancelButton(this), m_DBName(databaseName), m_DBTableName(dataTableName), m_operator(-1) { //打開(kāi)數(shù)據(jù)庫(kù) m_db = QSqlDatabase::addDatabase("QSQLITE"); m_db.setDatabaseName(QApplication::applicationDirPath() + "/config/" + databaseName); if (!m_db.open()) { m_DBName = ""; m_DBTableName = ""; } m_model = new QSqlTableModel(this, m_db); m_model->setTable(m_DBTableName); m_model->setEditStrategy(QSqlTableModel::OnManualSubmit); //手動(dòng)提交后才修改 m_model->select(); m_TabView = new QTableView(this); m_TabView->setEditTriggers(QAbstractItemView::NoEditTriggers); //設(shè)置內(nèi)容不可編輯 /*************關(guān)聯(lián)槽函數(shù)*********************/ connect(&m_NewButton, SIGNAL(clicked()), this, SLOT(onNewButtonClicked())); connect(&m_QueryButton, SIGNAL(clicked()), this, SLOT(onQueryButtonClicked())); connect(&m_UpdateButton, SIGNAL(clicked()), this, SLOT(onUpdateButtonClicked())); connect(&m_DeleteButton, SIGNAL(clicked()), this, SLOT(onDeleteButtonClicked())); connect(&m_PrimaryKeyLineEdit, SIGNAL(textChanged(const QString &)), this, SLOT(onPrimaryKeyLineEditEmpty(const QString &))); connect(m_TabView, SIGNAL(clicked(const QModelIndex &)), this, SLOT(onCurrentTableViewClicked(const QModelIndex &))); connect(&m_OKButton, SIGNAL(clicked()), this, SLOT(onOKButtonClicked())); connect(&m_CancelButton, SIGNAL(clicked()), this, SLOT(onCancelButtonClicked())); /*************模型關(guān)聯(lián)視圖*******************/ m_TabView->setModel(m_model); /*************選中行為為整行選中*************/ m_TabView->setSelectionBehavior(QAbstractItemView::SelectRows); /*************對(duì)話框窗體初始化***************/ UiInit(); /*************對(duì)話框窗體初始化***************/ setFixedSize(600, 400); setWindowTitle(QStringLiteral("用戶管理")); } /************************************************************************** * 函數(shù)名稱:UiInit * 函數(shù)功能:用戶管理對(duì)話框界面初始化 * 輸入?yún)?shù):無(wú) * 輸出參數(shù):無(wú) * 返回?cái)?shù)值:void * 創(chuàng)建人員: * 創(chuàng)建時(shí)間:2017-11-15 * 修改人員: * 修改時(shí)間: **************************************************************************/ void KSDemoDlg::UiInit() { m_PrimaryKeyLabel.setText(m_model->headerData(0, Qt::Horizontal).toString()); m_NewButton.setText(QStringLiteral("增加")); m_QueryButton.setText(QStringLiteral("查詢")); m_UpdateButton.setText(QStringLiteral("修改")); m_DeleteButton.setText(QStringLiteral("刪除")); m_UpdateButton.setEnabled(true); m_OKButton.setText(QStringLiteral("確定")); m_CancelButton.setText(QStringLiteral("取消")); /**************靈活增加界面右側(cè)數(shù)據(jù)顯示形式******************/ for(int i=0; i<m_model->columnCount(); i++) { m_infoLabelList.append(new QLabel(this)); m_infoLabelList[i]->setText(m_model->headerData(i, Qt::Horizontal).toString()); m_infoEditList.append(new QLineEdit(this)); m_infoEditList[i]->setEnabled(false); } m_OKButton.setEnabled(false); m_CancelButton.setEnabled(false); /**************靈活增加界面右側(cè)數(shù)據(jù)顯示形式 END******************/ QHBoxLayout *TotalHBoxLayout = new QHBoxLayout(); QVBoxLayout *TotalVBoxLayout = new QVBoxLayout(); QVBoxLayout *UserGroupVBoxLayout = new QVBoxLayout(); QHBoxLayout *UserEditHBoxLayout = new QHBoxLayout(); QHBoxLayout *UserButtonHBoxLayout = new QHBoxLayout(); QFormLayout *UserPrimaryKeyFormLayout = new QFormLayout(); QFormLayout *UserSelectFormLayout = new QFormLayout(); QHBoxLayout *UserSelectHBoxLayout = new QHBoxLayout(); QVBoxLayout *UserSelectVBoxLayout = new QVBoxLayout(); /*****************界面右側(cè)group布局******************/ for (int i=0; i<m_infoLabelList.count(); i++) { UserSelectFormLayout->addRow( m_infoLabelList[i], m_infoEditList[i]); } UserSelectHBoxLayout->addWidget(&m_OKButton); UserSelectHBoxLayout->addWidget(&m_CancelButton); UserSelectVBoxLayout->addLayout(UserSelectFormLayout); UserSelectVBoxLayout->addLayout(UserSelectHBoxLayout); UserSelectVBoxLayout->addStretch(); /*****************界面右側(cè)group布局 END******************/ UserPrimaryKeyFormLayout->addRow(&m_PrimaryKeyLabel, &m_PrimaryKeyLineEdit); UserEditHBoxLayout->addLayout(UserPrimaryKeyFormLayout); UserEditHBoxLayout->addWidget(&m_QueryButton); UserEditHBoxLayout->addStretch(); UserButtonHBoxLayout->addWidget(&m_NewButton); UserButtonHBoxLayout->addWidget(&m_UpdateButton); UserButtonHBoxLayout->addWidget(&m_DeleteButton); UserGroupVBoxLayout->addLayout(UserEditHBoxLayout); UserGroupVBoxLayout->addLayout(UserButtonHBoxLayout); m_Group.setLayout(UserGroupVBoxLayout); TotalVBoxLayout->addWidget(&m_Group); TotalVBoxLayout->addWidget(m_TabView); TotalHBoxLayout->addLayout(TotalVBoxLayout, 3); TotalHBoxLayout->addLayout(UserSelectVBoxLayout, 1); setLayout(TotalHBoxLayout); } /************************************************************************** * 函數(shù)名稱:onNewUserButtonClick * 函數(shù)功能:用戶管理對(duì)話框界新增用戶按鈕槽函數(shù) * 輸入?yún)?shù):無(wú) * 輸出參數(shù):無(wú) * 返回?cái)?shù)值:void * 創(chuàng)建人員: * 創(chuàng)建時(shí)間:2017-11-15 * 修改人員: * 修改時(shí)間: **************************************************************************/ void KSDemoDlg::onNewButtonClicked() { for (int i=0; i<m_infoEditList.count(); i++) { m_infoEditList[i]->setEnabled(true); } m_operator = INSERT; m_OKButton.setEnabled(true); m_CancelButton.setEnabled(true); } /************************************************************************** * 函數(shù)名稱:onQueryUserButtonClick * 函數(shù)功能:用戶管理對(duì)話框界查詢用戶按鈕槽函數(shù) * 輸入?yún)?shù):無(wú) * 輸出參數(shù):無(wú) * 返回?cái)?shù)值:void * 創(chuàng)建人員: * 創(chuàng)建時(shí)間:2017-11-15 * 修改人員: * 修改時(shí)間: **************************************************************************/ void KSDemoDlg::onQueryButtonClicked() { QString toFind = m_PrimaryKeyLineEdit.text(); QString ID = m_model->headerData(0, Qt::Horizontal).toString(); m_model->setFilter(ID + "=\'" + toFind + "\'"); m_model->select(); } /************************************************************************** * 函數(shù)名稱:onUpdateButtonClicked * 函數(shù)功能:用戶管理對(duì)話框界修改用戶按鈕槽函數(shù) * 輸入?yún)?shù):無(wú) * 輸出參數(shù):無(wú) * 返回?cái)?shù)值:void * 創(chuàng)建人員: * 創(chuàng)建時(shí)間:2017-11-15 * 修改人員: * 修改時(shí)間: **************************************************************************/ void KSDemoDlg::onUpdateButtonClicked() { int toUpdate = m_TabView->currentIndex().row(); QSqlRecord recode = m_model->record(toUpdate); for (int i=0; i<recode.count(); i++) { m_infoEditList[i]->setEnabled(true); m_infoEditList[i]->setText(recode.value(i).toString()); } m_operator = UPDATE; m_OKButton.setEnabled(true); m_CancelButton.setEnabled(true); } /************************************************************************** * 函數(shù)名稱:onDeleteButtonClicked * 函數(shù)功能:用戶管理對(duì)話框界刪除用戶按鈕槽函數(shù) * 輸入?yún)?shù):無(wú) * 輸出參數(shù):無(wú) * 返回?cái)?shù)值:void * 創(chuàng)建人員: * 創(chuàng)建時(shí)間:2017-11-15 * 修改人員: * 修改時(shí)間: **************************************************************************/ void KSDemoDlg::onDeleteButtonClicked() { int toDelRow = m_TabView->currentIndex().row(); if (QMessageBox::Ok == QMessageBox::warning(this, QStringLiteral("提示"), QStringLiteral("確定要?jiǎng)h除") + m_model->data(m_model->index(toDelRow, 0)).toString() + QStringLiteral("嗎?"), QMessageBox::Ok|QMessageBox::No)) { m_model->removeRow(toDelRow); m_model->submitAll(); } m_model->select(); } /************************************************************************** * 函數(shù)名稱:onUserNameEditEmpty * 函數(shù)功能:當(dāng)m_UserNameEdit編輯框?yàn)榭諘r(shí),顯示所有用戶 * 輸入?yún)?shù):無(wú) * 輸出參數(shù):無(wú) * 返回?cái)?shù)值:void * 創(chuàng)建人員: * 創(chuàng)建時(shí)間:2017-11-15 * 修改人員: * 修改時(shí)間: **************************************************************************/ void KSDemoDlg::onPrimaryKeyLineEditEmpty(const QString & text) { if (text.isEmpty()) { m_model->setTable(m_DBTableName); //重新關(guān)聯(lián)數(shù)據(jù)庫(kù)表,這樣才能查詢整個(gè)表 m_model->select(); } } /************************************************************************** * 函數(shù)名稱:onCurrentTableViewActived * 函數(shù)功能:m_TableView視圖選取當(dāng)前行槽函數(shù),內(nèi)容映射到右側(cè)用戶編輯中 * 輸入?yún)?shù):無(wú) * 輸出參數(shù):無(wú) * 返回?cái)?shù)值:void * 創(chuàng)建人員: * 創(chuàng)建時(shí)間:2017-11-15 * 修改人員: * 修改時(shí)間: **************************************************************************/ void KSDemoDlg::onCurrentTableViewClicked(const QModelIndex & index) { if (!m_OKButton.isEnabled() || (INSERT == m_operator)) //只有可編輯并且操作為修改操作時(shí)才映射內(nèi)容 { return; } int currentRow = index.row(); QSqlRecord recode = m_model->record(currentRow); for (int i=0; i<recode.count(); i++) { m_infoEditList[i]->setEnabled(true); m_infoEditList[i]->setText(recode.value(i).toString()); } } /************************************************************************** * 函數(shù)名稱:onOKButtonClicked * 函數(shù)功能:OKButton點(diǎn)擊槽函數(shù),確定修改數(shù)據(jù)庫(kù) * 輸入?yún)?shù):無(wú) * 輸出參數(shù):無(wú) * 返回?cái)?shù)值:void * 創(chuàng)建人員: * 創(chuàng)建時(shí)間:2017-11-15 * 修改人員: * 修改時(shí)間: **************************************************************************/ void KSDemoDlg::onOKButtonClicked() { for (int i=0; i<m_infoEditList.count(); i++) { if (m_infoEditList[i]->text().isEmpty()) { QMessageBox::warning(this, QStringLiteral("提示"), QStringLiteral("請(qǐng)將內(nèi)容填寫(xiě)完整"), QMessageBox::Ok); return; } } switch (m_operator) { case INSERT: { if (QMessageBox::Ok == QMessageBox::warning(this, QStringLiteral("提示"), QStringLiteral("請(qǐng)確定是否增加"), QMessageBox::Ok|QMessageBox::No)) { int col = m_model->columnCount(); int row = m_model->rowCount(); m_model->insertRow(row); for (int i=0; i<col; i++) { m_model->setData(m_model->index(row, i), m_infoEditList[i]->text()); } m_model->submitAll(); //提交修改 } } break; case UPDATE: { if (QMessageBox::Ok == QMessageBox::warning(this, QStringLiteral("提示"), QStringLiteral("請(qǐng)確定是否修改"), QMessageBox::Ok|QMessageBox::No)) { int col = m_model->columnCount(); int CurrentRow = m_TabView->currentIndex().row(); for (int i=0; i<col; i++) { m_model->setData(m_model->index(CurrentRow, i), m_infoEditList[i]->text()); } m_model->submitAll(); //提交修改 } } break; default: break; } for (int i=0; i<m_infoEditList.count(); i++) { m_infoEditList[i]->setText(""); m_infoEditList[i]->setEnabled(false); } m_model->select(); m_OKButton.setEnabled(false); m_CancelButton.setEnabled(false); } /************************************************************************** * 函數(shù)名稱:onCancelButtonClicked * 函數(shù)功能:OKButton點(diǎn)擊槽函數(shù),不操作 * 輸入?yún)?shù):無(wú) * 輸出參數(shù):無(wú) * 返回?cái)?shù)值:void * 創(chuàng)建人員: * 創(chuàng)建時(shí)間:2017-11-15 * 修改人員: * 修改時(shí)間: **************************************************************************/ void KSDemoDlg::onCancelButtonClicked() { for (int i=0; i<m_infoEditList.count(); i++) { m_infoEditList[i]->setText(""); m_infoEditList[i]->setEnabled(false); } m_OKButton.setEnabled(false); m_CancelButton.setEnabled(false); } /************************************************************************** * 函數(shù)名稱:~KsUserManageDlg * 函數(shù)功能:用戶管理對(duì)話框析構(gòu)函數(shù) * 輸入?yún)?shù):無(wú) * 輸出參數(shù):無(wú) * 返回?cái)?shù)值:void * 創(chuàng)建人員: * 創(chuàng)建時(shí)間:2017-11-15 * 修改人員: * 修改時(shí)間: **************************************************************************/ KSDemoDlg::~KSDemoDlg() { qDebug() << "KSDemoDlg::~KSDemoDlg()"; m_db.close(); }
main函數(shù)
#include "KsTestDemo.h" #include <QtWidgets/QApplication> #include <QCoreApplication> #include "KSDemoDlg.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); KSDemoDlg dlg("CONFIG.db", "T_USER_MANAGE"); //這里我們?cè)谏蒏SDemoDlg類(lèi)的時(shí)候,在構(gòu)造函數(shù)中傳入sqlite數(shù)據(jù)庫(kù)名CONFIG.DB和想要操作的表T_USER_MANAGE dlg.show(); //顯示一下就OK return a.exec(); }
上邊的 KSDemoDlg dlg("CONFIG.db", "T_USER_MANAGE");數(shù)據(jù)庫(kù)名跟表也可以換成其他的,代碼通用。
以上就是QT讀寫(xiě)Sqlite數(shù)據(jù)庫(kù)的三種方式的詳細(xì)內(nèi)容,更多關(guān)于QT讀寫(xiě)Sqlite數(shù)據(jù)庫(kù)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C語(yǔ)言實(shí)現(xiàn)動(dòng)態(tài)版通訊錄的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用C語(yǔ)言實(shí)現(xiàn)一個(gè)簡(jiǎn)單的動(dòng)態(tài)版通訊錄,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C語(yǔ)言有一定幫助,需要的可以參考一下2022-08-08C++中function的實(shí)現(xiàn)原理詳解
類(lèi)模版std::function是一種通用、多態(tài)的函數(shù)封裝。function的實(shí)例可以對(duì)任何可以調(diào)用的目標(biāo)實(shí)體進(jìn)行存儲(chǔ)、復(fù)制、和調(diào)用操作。本文主要聊聊它的實(shí)現(xiàn)原理,需要的可以參考一下2022-12-12簡(jiǎn)要解讀C++的動(dòng)態(tài)和靜態(tài)關(guān)聯(lián)以及虛析構(gòu)函數(shù)
這篇文章主要介紹了簡(jiǎn)要解讀C++的動(dòng)態(tài)和靜態(tài)關(guān)聯(lián)以及虛析構(gòu)函數(shù),析構(gòu)函數(shù)在C++編程中平時(shí)并不是太常用,需要的朋友可以參考下2015-09-09C++ vector模擬實(shí)現(xiàn)的代碼詳解
vector是表示可變大小數(shù)組的序列容器,就像數(shù)組一樣,vector也采用的連續(xù)存儲(chǔ)空間來(lái)存儲(chǔ)元素,本質(zhì)講,vector使用動(dòng)態(tài)分配數(shù)組來(lái)存儲(chǔ)它的元素,本文將給大家詳細(xì)介紹一下C++ vector模擬實(shí)現(xiàn),需要的朋友可以參考下2023-07-07C++非遞歸隊(duì)列實(shí)現(xiàn)二叉樹(shù)的廣度優(yōu)先遍歷
這篇文章主要介紹了C++非遞歸隊(duì)列實(shí)現(xiàn)二叉樹(shù)的廣度優(yōu)先遍歷,實(shí)例分析了遍歷二叉樹(shù)相關(guān)算法技巧,并附帶了兩個(gè)相關(guān)算法實(shí)例,需要的朋友可以參考下2015-07-07C++實(shí)現(xiàn)LeetCode(21.混合插入有序鏈表)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(21.混合插入有序鏈表),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07