QT读写Sqlite数据库三种方式

  • Post author:
  • Post category:其他


QT对一些基本的数据库的访问封装,可谓是极大的方便的我们开发人员,现在我们就来说下QT对Sqlite这个数据库的读写,Sqlite是一个比较小型的本地数据库,对于保存一些软件配置参数或量不是很大的数据是相当的方便,Qt本身已经自带了Sqlite的驱动,直接使用相关的类库即可,这篇我们主要来说明QT访问Sqlite数据库的三种方式(即使用三种类库去访问),分别为QSqlQuery、QSqlQueryModel、QSqlTableModel,对于这三种类库,可看为一个比一个上层,也就是封装的更厉害,甚至第三种QSqlTableModel,根本就不需要开发者懂SQL语言,也能操作Sqlite数据库。


1、首先使用QSqlQuery来访问


我们先要在工程中包含与数据库相关的几个头文件#include <QtSql/QSqlDatabase> 、#include <QtSql/QSqlRecord>、#include <QtSql/QSqlQuery>

访问的数据库内容结构为:

 

  1. #include <QtWidgets/QApplication>


  2. #include <QCoreApplication>


  3. #include <QDebug>


  4. #include <QtSql/QSqlDatabase>


  5. #include <QtSql/QSqlQuery>


  6. #include <QtSql/QSqlRecord>


  7. typedef struct _testInfo //假定数据库存储内容


  8. {


  9. QString UsreName;


  10. QString IP;


  11. QString Port;


  12. QString PassWord;


  13. QString Type;


  14. }testInfo;


  15. int main(int argc, char *argv[])


  16. {


  17. QApplication a(argc, argv);


  18. QVector<testInfo> infoVect; //testInfo向量,用于存储数据库查询到的数据


  19. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");


  20. db.setDatabaseName(QApplication::applicationDirPath() + "/CONFIG/" + "CONFIG.db");


  21. if (!db.open())


  22. {


  23. return 0;


  24. }


  25. /**************************使用QSqlQuery操作数据库**************************/


  26. QSqlQuery query; //执行操作类对象


  27. //查询数据


  28. query.prepare("SELECT * FROM T_USER_MANAGE");


  29. query.exec(); //执行


  30. QSqlRecord recode = query.record(); //recode保存查询到一些内容信息,如表头、列数等等


  31. int column = recode.count(); //获取读取结果的列数


  32. QString s1 = recode.fieldName(0); //获取第0列的列名


  33. while (query.next())


  34. {


  35. testInfo tmp;


  36. tmp.UsreName = query.value("UsreName").toString();


  37. tmp.IP = query.value("IP").toString();


  38. tmp.Port = query.value("Port").toString();


  39. tmp.PassWord = query.value("PassWord").toString();


  40. tmp.Type = query.value("Type").toString();


  41. infoVect.push_back(tmp); //将查询到的内容存到testInfo向量中


  42. }


  43. for (int i=0; i<infoVect.size(); i++) //打印输出


  44. {


  45. qDebug() << infoVect[i].UsreName << ":" \


  46. << infoVect[i].IP << ":" \


  47. << infoVect[i].Port << ":" \


  48. << infoVect[i].PassWord << ":" \


  49. << infoVect[i].Type;


  50. }


  51. //插入数据


  52. query.prepare("INSERT INTO T_USER_MANAGE (UsreName, IP, Port, PassWord, Type) VALUES (:UsreName, :IP, :Port, :PassWord, :Type)");


  53. query.bindValue(":UserName", "user4"); //给每个插入值标识符设定具体值


  54. query.bindValue(":IP", "192.168.1.5");


  55. query.bindValue(":Port", "5004");


  56. query.bindValue(":PassWord", "55555");


  57. query.bindValue(":Type", "operator");


  58. query.exec();


  59. //更改表中 UserName=user4 的Type属性为admin


  60. query.prepare("UPDATE T_USER_MANAGE SET Type='admin' WHERE UserName='user4'");


  61. query.exec();


  62. //删除表中 UserName=user4的用户信息


  63. query.prepare("DELETE FROM T_USER_MANAGE WHERE UserName='user4'");


  64. query.exec();


  65. #endif


  66. /**************************使用QSqlQuery操作数据库END***********************/

编译输出:


2、使用QSqlQueryModel来访问



QSqlQueryModel类带有Model字样,相信你已经猜到我们可以用他来关联试图,就能把数据库的内容显示到视图上,当然,常规的操作也是可以的,但是我们只说说怎么用这个类来把数据库中的内容显示到是视图中,这里我们选择的视图类为QTableView,直接上代码吧

 

  1. #include <QtWidgets/QApplication>


  2. #include <QCoreApplication>


  3. #include <QDebug>


  4. #include <QString>


  5. #include <QTableView>


  6. #include <QtSql/QSqlDatabase>


  7. #include <QtSql/QSqlQueryModel>


  8. int main(int argc, char *argv[])


  9. {


  10. QApplication a(argc, argv);


  11. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");


  12. db.setDatabaseName(QApplication::applicationDirPath() + "/CONFIG/" + "CONFIG.db");


  13. if (!db.open())


  14. {


  15. return 0;


  16. }


  17. QSqlQueryModel *model = new QSqlQueryModel;


  18. model->setQuery("SELECT * FROM T_USER_MANAGE", db); //从给定的数据库db执行sql操作, db需预先制定并打开


  19. int column = model->columnCount(); //获取列数


  20. int row = model->rowCount(); //获取行数


  21. model->setHeaderData(0, Qt::Horizontal, QStringLiteral("用户名")); //设置表头,如不设置则使用数据库中的默认表头


  22. model->setHeaderData(1, Qt::Horizontal, QStringLiteral("IP地址"));


  23. model->setHeaderData(2, Qt::Horizontal, QStringLiteral("端口"));


  24. model->setHeaderData(3, Qt::Horizontal, QStringLiteral("密码"));


  25. model->setHeaderData(4, Qt::Horizontal, QStringLiteral("用户类型"));


  26. QTableView *view = new QTableView; //定义视图,只能用于显示,不能修改数据库


  27. view->setFixedSize(500, 200);


  28. view->setModel(model);


  29. view->show();


  30. return a.exec();


  31. }

编译运行一下:


3、最后使用QSqlTableModel来访问


最后我们来说说使用QSqlTableModel这个类去操作Sqlite数据库,这个类比上两个封装的更彻底,即使我们不懂SQL语言,也能实现对Sqlite数据库的操作,并且这个类也可以通过视图来显示修改数据库内容,这里我就拿这个类做了个用户管理模块,其实也可以通用与其他任何一个模块,只要在生成对象时传入sqlite的数据库名及要操作的表名即可。

在这个例子中,我实现了一个KSDemoDlg类,其实是一个对话框类,里边包含了sqlite数据库的显示、修改等等功能,首先来看下效果(常规的增删改查功能都有):

当我们点击增加、修改时,右边的编辑框便为可编辑状态(说明下,右边的编辑框是随着加载的数据库表变化而变化的,简而言之就是可以不做修改就能操作别的Sqlite数据库),完毕确定后便写进数据库,同时也在左边的表格中显示

头文件:

 

  1. #ifndef __KSDEMODLG_H__


  2. #define __KSDEMODLG_H__


  3. #include <QDialog>


  4. #include <QPushButton>


  5. #include <QLineEdit>


  6. #include <QLabel>


  7. #include <QComboBox>


  8. #include <QGroupBox>


  9. #include <QTableView>


  10. #include <QtSql/QSqlTableModel>


  11. #include <QtSql/QSqlDatabase>


  12. class KSDemoDlg : public QDialog


  13. {


  14. Q_OBJECT


  15. enum {UPDATE, INSERT};


  16. int m_operator;


  17. public:


  18. explicit KSDemoDlg(QString databaseName, QString dataTableName, QWidget *parent = 0 );


  19. ~KSDemoDlg();


  20. private:


  21. void UiInit();


  22. protected slots:


  23. void onNewButtonClicked();


  24. void onQueryButtonClicked();


  25. void onUpdateButtonClicked();


  26. void onDeleteButtonClicked();


  27. void onPrimaryKeyLineEditEmpty(const QString & text);


  28. void onCurrentTableViewClicked(const QModelIndex & index);


  29. void onOKButtonClicked();


  30. void onCancelButtonClicked();


  31. private:


  32. QSqlDatabase m_db;


  33. QString m_DBName;


  34. QString m_DBTableName;


  35. private:


  36. QTableView* m_TabView;


  37. QSqlTableModel* m_model;


  38. private:


  39. QList<QLineEdit*> m_infoEditList;


  40. QList<QLabel*> m_infoLabelList;


  41. QPushButton m_OKButton;


  42. QPushButton m_CancelButton;


  43. private:


  44. /*所有用户信息容器组*/


  45. QGroupBox m_Group;


  46. QLabel m_PrimaryKeyLabel;


  47. QLineEdit m_PrimaryKeyLineEdit;


  48. QPushButton m_QueryButton;


  49. QPushButton m_NewButton;


  50. QPushButton m_UpdateButton;


  51. QPushButton m_DeleteButton;


  52. /*所选择用户信息容器组*/


  53. QGroupBox m_SelectGroup;


  54. };


  55. #endif // __KSDEMODLG_H__

.cpp文件

 

  1. #include <QtWidgets/QApplication>


  2. #include <QCoreApplication>


  3. #include <QString>


  4. #include <QFormLayout>


  5. #include <QVBoxLayout>


  6. #include <QHBoxLayout>


  7. #include <QMessageBox>


  8. #include <QtSql/QSqlRecord>


  9. #include <QDebug>


  10. #include "KSDemoDlg.h"


  11. /**************************************************************************


  12. * 函数名称:KSDemoDlg


  13. * 函数功能:用户管理对话框构造函数


  14. * 输入参数:无


  15. * 输出参数:无


  16. * 返回数值:void


  17. * 创建人员:


  18. * 创建时间:2017-11-15


  19. * 修改人员:


  20. * 修改时间:


  21. **************************************************************************/


  22. KSDemoDlg::KSDemoDlg(QString databaseName, QString dataTableName, QWidget *parent):QDialog(parent, Qt::WindowCloseButtonHint | Qt::WindowMinMaxButtonsHint | Qt::WindowStaysOnTopHint),


  23. 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),


  24. m_OKButton(this),m_CancelButton(this), m_DBName(databaseName), m_DBTableName(dataTableName), m_operator(-1)


  25. {


  26. //打开数据库


  27. m_db = QSqlDatabase::addDatabase("QSQLITE");


  28. m_db.setDatabaseName(QApplication::applicationDirPath() + "/config/" + databaseName);


  29. if (!m_db.open())


  30. {


  31. m_DBName = "";


  32. m_DBTableName = "";


  33. }


  34. m_model = new QSqlTableModel(this, m_db);


  35. m_model->setTable(m_DBTableName);


  36. m_model->setEditStrategy(QSqlTableModel::OnManualSubmit); //手动提交后才修改


  37. m_model->select();


  38. m_TabView = new QTableView(this);


  39. m_TabView->setEditTriggers(QAbstractItemView::NoEditTriggers); //设置内容不可编辑


  40. /*************关联槽函数*********************/


  41. connect(&m_NewButton, SIGNAL(clicked()), this, SLOT(onNewButtonClicked()));


  42. connect(&m_QueryButton, SIGNAL(clicked()), this, SLOT(onQueryButtonClicked()));


  43. connect(&m_UpdateButton, SIGNAL(clicked()), this, SLOT(onUpdateButtonClicked()));


  44. connect(&m_DeleteButton, SIGNAL(clicked()), this, SLOT(onDeleteButtonClicked()));


  45. connect(&m_PrimaryKeyLineEdit, SIGNAL(textChanged(const QString &)), this, SLOT(onPrimaryKeyLineEditEmpty(const QString &)));


  46. connect(m_TabView, SIGNAL(clicked(const QModelIndex &)), this, SLOT(onCurrentTableViewClicked(const QModelIndex &)));


  47. connect(&m_OKButton, SIGNAL(clicked()), this, SLOT(onOKButtonClicked()));


  48. connect(&m_CancelButton, SIGNAL(clicked()), this, SLOT(onCancelButtonClicked()));


  49. /*************模型关联视图*******************/


  50. m_TabView->setModel(m_model);


  51. /*************选中行为为整行选中*************/


  52. m_TabView->setSelectionBehavior(QAbstractItemView::SelectRows);


  53. /*************对话框窗体初始化***************/


  54. UiInit();


  55. /*************对话框窗体初始化***************/


  56. setFixedSize(600, 400);


  57. setWindowTitle(QStringLiteral("用户管理"));


  58. }


  59. /**************************************************************************


  60. * 函数名称:UiInit


  61. * 函数功能:用户管理对话框界面初始化


  62. * 输入参数:无


  63. * 输出参数:无


  64. * 返回数值:void


  65. * 创建人员:


  66. * 创建时间:2017-11-15


  67. * 修改人员:


  68. * 修改时间:


  69. **************************************************************************/


  70. void KSDemoDlg::UiInit()


  71. {


  72. m_PrimaryKeyLabel.setText(m_model->headerData(0, Qt::Horizontal).toString());


  73. m_NewButton.setText(QStringLiteral("增加"));


  74. m_QueryButton.setText(QStringLiteral("查询"));


  75. m_UpdateButton.setText(QStringLiteral("修改"));


  76. m_DeleteButton.setText(QStringLiteral("删除"));


  77. m_UpdateButton.setEnabled(true);


  78. m_OKButton.setText(QStringLiteral("确定"));


  79. m_CancelButton.setText(QStringLiteral("取消"));


  80. /**************灵活增加界面右侧数据显示形式******************/


  81. for(int i=0; i<m_model->columnCount(); i++)


  82. {


  83. m_infoLabelList.append(new QLabel(this));


  84. m_infoLabelList[i]->setText(m_model->headerData(i, Qt::Horizontal).toString());


  85. m_infoEditList.append(new QLineEdit(this));


  86. m_infoEditList[i]->setEnabled(false);


  87. }


  88. m_OKButton.setEnabled(false);


  89. m_CancelButton.setEnabled(false);


  90. /**************灵活增加界面右侧数据显示形式 END******************/


  91. QHBoxLayout *TotalHBoxLayout = new QHBoxLayout();


  92. QVBoxLayout *TotalVBoxLayout = new QVBoxLayout();


  93. QVBoxLayout *UserGroupVBoxLayout = new QVBoxLayout();


  94. QHBoxLayout *UserEditHBoxLayout = new QHBoxLayout();


  95. QHBoxLayout *UserButtonHBoxLayout = new QHBoxLayout();


  96. QFormLayout *UserPrimaryKeyFormLayout = new QFormLayout();


  97. QFormLayout *UserSelectFormLayout = new QFormLayout();


  98. QHBoxLayout *UserSelectHBoxLayout = new QHBoxLayout();


  99. QVBoxLayout *UserSelectVBoxLayout = new QVBoxLayout();


  100. /*****************界面右侧group布局******************/


  101. for (int i=0; i<m_infoLabelList.count(); i++)


  102. {


  103. UserSelectFormLayout->addRow( m_infoLabelList[i], m_infoEditList[i]);


  104. }


  105. UserSelectHBoxLayout->addWidget(&m_OKButton);


  106. UserSelectHBoxLayout->addWidget(&m_CancelButton);


  107. UserSelectVBoxLayout->addLayout(UserSelectFormLayout);


  108. UserSelectVBoxLayout->addLayout(UserSelectHBoxLayout);


  109. UserSelectVBoxLayout->addStretch();


  110. /*****************界面右侧group布局 END******************/


  111. UserPrimaryKeyFormLayout->addRow(&m_PrimaryKeyLabel, &m_PrimaryKeyLineEdit);


  112. UserEditHBoxLayout->addLayout(UserPrimaryKeyFormLayout);


  113. UserEditHBoxLayout->addWidget(&m_QueryButton);


  114. UserEditHBoxLayout->addStretch();


  115. UserButtonHBoxLayout->addWidget(&m_NewButton);


  116. UserButtonHBoxLayout->addWidget(&m_UpdateButton);


  117. UserButtonHBoxLayout->addWidget(&m_DeleteButton);


  118. UserGroupVBoxLayout->addLayout(UserEditHBoxLayout);


  119. UserGroupVBoxLayout->addLayout(UserButtonHBoxLayout);


  120. m_Group.setLayout(UserGroupVBoxLayout);


  121. TotalVBoxLayout->addWidget(&m_Group);


  122. TotalVBoxLayout->addWidget(m_TabView);


  123. TotalHBoxLayout->addLayout(TotalVBoxLayout, 3);


  124. TotalHBoxLayout->addLayout(UserSelectVBoxLayout, 1);


  125. setLayout(TotalHBoxLayout);


  126. }


  127. /**************************************************************************


  128. * 函数名称:onNewUserButtonClick


  129. * 函数功能:用户管理对话框界新增用户按钮槽函数


  130. * 输入参数:无


  131. * 输出参数:无


  132. * 返回数值:void


  133. * 创建人员:


  134. * 创建时间:2017-11-15


  135. * 修改人员:


  136. * 修改时间:


  137. **************************************************************************/


  138. void KSDemoDlg::onNewButtonClicked()


  139. {


  140. for (int i=0; i<m_infoEditList.count(); i++)


  141. {


  142. m_infoEditList[i]->setEnabled(true);


  143. }


  144. m_operator = INSERT;


  145. m_OKButton.setEnabled(true);


  146. m_CancelButton.setEnabled(true);


  147. }


  148. /**************************************************************************


  149. * 函数名称:onQueryUserButtonClick


  150. * 函数功能:用户管理对话框界查询用户按钮槽函数


  151. * 输入参数:无


  152. * 输出参数:无


  153. * 返回数值:void


  154. * 创建人员:廖明胜


  155. * 创建时间:2017-11-15


  156. * 修改人员:


  157. * 修改时间:


  158. **************************************************************************/


  159. void KSDemoDlg::onQueryButtonClicked()


  160. {


  161. QString toFind = m_PrimaryKeyLineEdit.text();


  162. QString ID = m_model->headerData(0, Qt::Horizontal).toString();


  163. m_model->setFilter(ID + "=\'" + toFind + "\'");


  164. m_model->select();


  165. }


  166. /**************************************************************************


  167. * 函数名称:onUpdateButtonClicked


  168. * 函数功能:用户管理对话框界修改用户按钮槽函数


  169. * 输入参数:无


  170. * 输出参数:无


  171. * 返回数值:void


  172. * 创建人员:


  173. * 创建时间:2017-11-15


  174. * 修改人员:


  175. * 修改时间:


  176. **************************************************************************/


  177. void KSDemoDlg::onUpdateButtonClicked()


  178. {


  179. int toUpdate = m_TabView->currentIndex().row();


  180. QSqlRecord recode = m_model->record(toUpdate);


  181. for (int i=0; i<recode.count(); i++)


  182. {


  183. m_infoEditList[i]->setEnabled(true);


  184. m_infoEditList[i]->setText(recode.value(i).toString());


  185. }


  186. m_operator = UPDATE;


  187. m_OKButton.setEnabled(true);


  188. m_CancelButton.setEnabled(true);


  189. }


  190. /**************************************************************************


  191. * 函数名称:onDeleteButtonClicked


  192. * 函数功能:用户管理对话框界删除用户按钮槽函数


  193. * 输入参数:无


  194. * 输出参数:无


  195. * 返回数值:void


  196. * 创建人员:


  197. * 创建时间:2017-11-15


  198. * 修改人员:


  199. * 修改时间:


  200. **************************************************************************/


  201. void KSDemoDlg::onDeleteButtonClicked()


  202. {


  203. int toDelRow = m_TabView->currentIndex().row();


  204. if (QMessageBox::Ok == QMessageBox::warning(this, QStringLiteral("提示"), QStringLiteral("确定要删除") + m_model->data(m_model->index(toDelRow, 0)).toString() + QStringLiteral("吗?"), QMessageBox::Ok|QMessageBox::No))


  205. {


  206. m_model->removeRow(toDelRow);


  207. m_model->submitAll();


  208. }


  209. m_model->select();


  210. }


  211. /**************************************************************************


  212. * 函数名称:onUserNameEditEmpty


  213. * 函数功能:当m_UserNameEdit编辑框为空时,显示所有用户


  214. * 输入参数:无


  215. * 输出参数:无


  216. * 返回数值:void


  217. * 创建人员:


  218. * 创建时间:2017-11-15


  219. * 修改人员:


  220. * 修改时间:


  221. **************************************************************************/


  222. void KSDemoDlg::onPrimaryKeyLineEditEmpty(const QString & text)


  223. {


  224. if (text.isEmpty())


  225. {


  226. m_model->setTable(m_DBTableName); //重新关联数据库表,这样才能查询整个表


  227. m_model->select();


  228. }


  229. }


  230. /**************************************************************************


  231. * 函数名称:onCurrentTableViewActived


  232. * 函数功能:m_TableView视图选取当前行槽函数,内容映射到右侧用户编辑中


  233. * 输入参数:无


  234. * 输出参数:无


  235. * 返回数值:void


  236. * 创建人员:


  237. * 创建时间:2017-11-15


  238. * 修改人员:


  239. * 修改时间:


  240. **************************************************************************/


  241. void KSDemoDlg::onCurrentTableViewClicked(const QModelIndex & index)


  242. {


  243. if (!m_OKButton.isEnabled() || (INSERT == m_operator)) //只有可编辑并且操作为修改操作时才映射内容


  244. {


  245. return;


  246. }


  247. int currentRow = index.row();


  248. QSqlRecord recode = m_model->record(currentRow);


  249. for (int i=0; i<recode.count(); i++)


  250. {


  251. m_infoEditList[i]->setEnabled(true);


  252. m_infoEditList[i]->setText(recode.value(i).toString());


  253. }


  254. }


  255. /**************************************************************************


  256. * 函数名称:onOKButtonClicked


  257. * 函数功能:OKButton点击槽函数,确定修改数据库


  258. * 输入参数:无


  259. * 输出参数:无


  260. * 返回数值:void


  261. * 创建人员:


  262. * 创建时间:2017-11-15


  263. * 修改人员:


  264. * 修改时间:


  265. **************************************************************************/


  266. void KSDemoDlg::onOKButtonClicked()


  267. {


  268. for (int i=0; i<m_infoEditList.count(); i++)


  269. {


  270. if (m_infoEditList[i]->text().isEmpty())


  271. {


  272. QMessageBox::warning(this, QStringLiteral("提示"), QStringLiteral("请将内容填写完整"), QMessageBox::Ok);


  273. return;


  274. }


  275. }


  276. switch (m_operator)


  277. {


  278. case INSERT:


  279. {


  280. if (QMessageBox::Ok == QMessageBox::warning(this, QStringLiteral("提示"), QStringLiteral("请确定是否增加"), QMessageBox::Ok|QMessageBox::No))


  281. {


  282. int col = m_model->columnCount();


  283. int row = m_model->rowCount();


  284. m_model->insertRow(row);


  285. for (int i=0; i<col; i++)


  286. {


  287. m_model->setData(m_model->index(row, i), m_infoEditList[i]->text());


  288. }


  289. m_model->submitAll(); //提交修改


  290. }


  291. }


  292. break;


  293. case UPDATE:


  294. {


  295. if (QMessageBox::Ok == QMessageBox::warning(this, QStringLiteral("提示"), QStringLiteral("请确定是否修改"), QMessageBox::Ok|QMessageBox::No))


  296. {


  297. int col = m_model->columnCount();


  298. int CurrentRow = m_TabView->currentIndex().row();


  299. for (int i=0; i<col; i++)


  300. {


  301. m_model->setData(m_model->index(CurrentRow, i), m_infoEditList[i]->text());


  302. }


  303. m_model->submitAll(); //提交修改


  304. }


  305. }


  306. break;


  307. default:


  308. break;


  309. }


  310. for (int i=0; i<m_infoEditList.count(); i++)


  311. {


  312. m_infoEditList[i]->setText("");


  313. m_infoEditList[i]->setEnabled(false);


  314. }


  315. m_model->select();


  316. m_OKButton.setEnabled(false);


  317. m_CancelButton.setEnabled(false);


  318. }


  319. /**************************************************************************


  320. * 函数名称:onCancelButtonClicked


  321. * 函数功能:OKButton点击槽函数,不操作


  322. * 输入参数:无


  323. * 输出参数:无


  324. * 返回数值:void


  325. * 创建人员:


  326. * 创建时间:2017-11-15


  327. * 修改人员:


  328. * 修改时间:


  329. **************************************************************************/


  330. void KSDemoDlg::onCancelButtonClicked()


  331. {


  332. for (int i=0; i<m_infoEditList.count(); i++)


  333. {


  334. m_infoEditList[i]->setText("");


  335. m_infoEditList[i]->setEnabled(false);


  336. }


  337. m_OKButton.setEnabled(false);


  338. m_CancelButton.setEnabled(false);


  339. }


  340. /**************************************************************************


  341. * 函数名称:~KsUserManageDlg


  342. * 函数功能:用户管理对话框析构函数


  343. * 输入参数:无


  344. * 输出参数:无


  345. * 返回数值:void


  346. * 创建人员:


  347. * 创建时间:2017-11-15


  348. * 修改人员:


  349. * 修改时间:


  350. **************************************************************************/


  351. KSDemoDlg::~KSDemoDlg()


  352. {


  353. qDebug() << "KSDemoDlg::~KSDemoDlg()";


  354. m_db.close();


  355. }

main函数

 

  1. #include "KsTestDemo.h"


  2. #include <QtWidgets/QApplication>


  3. #include <QCoreApplication>


  4. #include "KSDemoDlg.h"


  5. int main(int argc, char *argv[])


  6. {


  7. QApplication a(argc, argv);


  8. KSDemoDlg dlg("CONFIG.db", "T_USER_MANAGE"); //这里我们在生成KSDemoDlg类的时候,在构造函数中传入sqlite数据库名CONFIG.DB和想要操作的表T_USER_MANAGE


  9. dlg.show(); //显示一下就OK


  10. return a.exec();


  11. }

上边的 KSDemoDlg dlg(“CONFIG.db”, “T_USER_MANAGE”);数据库名跟表也可以换成其他的,代码通用。



版权声明:本文为u011555996原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。