QT读写Sqlite数据库三种方式

71 篇文章 15 订阅

 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.  
  5. #include <QtSql/QSqlDatabase>

  6. #include <QtSql/QSqlQuery>

  7. #include <QtSql/QSqlRecord>

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

  10. {

  11. QString UsreName;

  12. QString IP;

  13. QString Port;

  14. QString PassWord;

  15. QString Type;

  16.  
  17. }testInfo;

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

  20. {

  21. QApplication a(argc, argv);

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

  24.  
  25. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");

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

  28. if (!db.open())

  29. {

  30. return 0;

  31. }

  32.  
  33. /**************************使用QSqlQuery操作数据库**************************/

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

  35.  
  36. //查询数据

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

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

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

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

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

  43.  
  44. while (query.next())

  45. {

  46. testInfo tmp;

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

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

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

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

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

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

  54. }

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

  57. {

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

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

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

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

  62. << infoVect[i].Type;

  63. }

  64.  
  65. //插入数据

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

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

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

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

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

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

  72. query.exec();

  73.  
  74.  
  75. //更改表中 UserName=user4 的Type属性为admin

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

  77. query.exec();

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

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

  81. query.exec();

  82.  
  83. #endif

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

编译输出:

2、使用QSqlQueryModel来访问

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

 
  1. #include <QtWidgets/QApplication>

  2. #include <QCoreApplication>

  3. #include <QDebug>

  4. #include <QString>

  5. #include <QTableView>

  6.  
  7. #include <QtSql/QSqlDatabase>

  8. #include <QtSql/QSqlQueryModel>

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

  11. {

  12. QApplication a(argc, argv);

  13.  
  14. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");

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

  17. if (!db.open())

  18. {

  19. return 0;

  20. }

  21.  
  22. QSqlQueryModel *model = new QSqlQueryModel;

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

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

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

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

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

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

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

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

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

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

  36. view->setModel(model);

  37.  
  38. view->show();

  39.  
  40. return a.exec();

  41. }

编译运行一下:

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

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

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

头文件:

 
  1. #ifndef __KSDEMODLG_H__

  2. #define __KSDEMODLG_H__

  3.  
  4. #include <QDialog>

  5. #include <QPushButton>

  6. #include <QLineEdit>

  7. #include <QLabel>

  8. #include <QComboBox>

  9. #include <QGroupBox>

  10. #include <QTableView>

  11. #include <QtSql/QSqlTableModel>

  12. #include <QtSql/QSqlDatabase>

  13.  
  14.  
  15. class KSDemoDlg : public QDialog

  16. {

  17. Q_OBJECT

  18.  
  19. enum {UPDATE, INSERT};

  20. int m_operator;

  21.  
  22. public:

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

  24. ~KSDemoDlg();

  25.  
  26. private:

  27.  
  28. void UiInit();

  29.  
  30. protected slots:

  31. void onNewButtonClicked();

  32. void onQueryButtonClicked();

  33. void onUpdateButtonClicked();

  34. void onDeleteButtonClicked();

  35. void onPrimaryKeyLineEditEmpty(const QString & text);

  36. void onCurrentTableViewClicked(const QModelIndex & index);

  37. void onOKButtonClicked();

  38. void onCancelButtonClicked();

  39.  
  40. private:

  41. QSqlDatabase m_db;

  42. QString m_DBName;

  43. QString m_DBTableName;

  44.  
  45. private:

  46. QTableView* m_TabView;

  47. QSqlTableModel* m_model;

  48.  
  49. private:

  50. QList<QLineEdit*> m_infoEditList;

  51. QList<QLabel*> m_infoLabelList;

  52. QPushButton m_OKButton;

  53. QPushButton m_CancelButton;

  54.  
  55. private:

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

  58. QGroupBox m_Group;

  59.  
  60. QLabel m_PrimaryKeyLabel;

  61. QLineEdit m_PrimaryKeyLineEdit;

  62. QPushButton m_QueryButton;

  63.  
  64. QPushButton m_NewButton;

  65. QPushButton m_UpdateButton;

  66. QPushButton m_DeleteButton;

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

  69. QGroupBox m_SelectGroup;

  70.  
  71. };

  72.  
  73. #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.  
  11. #include "KSDemoDlg.h"

  12.  
  13.  
  14. /**************************************************************************

  15. * 函数名称:KSDemoDlg

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

  17. * 输入参数:无

  18. * 输出参数:无

  19. * 返回数值:void

  20. * 创建人员:

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

  22. * 修改人员:

  23. * 修改时间:

  24. **************************************************************************/

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

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

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

  28. {

  29. //打开数据库

  30.  
  31. m_db = QSqlDatabase::addDatabase("QSQLITE");

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

  33. if (!m_db.open())

  34. {

  35. m_DBName = "";

  36. m_DBTableName = "";

  37. }

  38.  
  39. m_model = new QSqlTableModel(this, m_db);

  40. m_model->setTable(m_DBTableName);

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

  42.  
  43. m_model->select();

  44.  
  45. m_TabView = new QTableView(this);

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

  47.  
  48. /*************关联槽函数*********************/

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

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

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

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

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

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

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

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

  57.  
  58. /*************模型关联视图*******************/

  59.  
  60. m_TabView->setModel(m_model);

  61.  
  62. /*************选中行为为整行选中*************/

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

  64.  
  65. /*************对话框窗体初始化***************/

  66. UiInit();

  67.  
  68. /*************对话框窗体初始化***************/

  69. setFixedSize(600, 400);

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

  71. }

  72.  
  73. /**************************************************************************

  74. * 函数名称:UiInit

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

  76. * 输入参数:无

  77. * 输出参数:无

  78. * 返回数值:void

  79. * 创建人员:

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

  81. * 修改人员:

  82. * 修改时间:

  83. **************************************************************************/

  84.  
  85. void KSDemoDlg::UiInit()

  86. {

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

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

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

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

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

  92. m_UpdateButton.setEnabled(true);

  93.  
  94. m_OKButton.setText(QStringLiteral("确定"));

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

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

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

  99. {

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

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

  102.  
  103. m_infoEditList.append(new QLineEdit(this));

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

  105. }

  106. m_OKButton.setEnabled(false);

  107. m_CancelButton.setEnabled(false);

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

  110.  
  111. QHBoxLayout *TotalHBoxLayout = new QHBoxLayout();

  112. QVBoxLayout *TotalVBoxLayout = new QVBoxLayout();

  113.  
  114. QVBoxLayout *UserGroupVBoxLayout = new QVBoxLayout();

  115.  
  116. QHBoxLayout *UserEditHBoxLayout = new QHBoxLayout();

  117. QHBoxLayout *UserButtonHBoxLayout = new QHBoxLayout();

  118.  
  119. QFormLayout *UserPrimaryKeyFormLayout = new QFormLayout();

  120.  
  121. QFormLayout *UserSelectFormLayout = new QFormLayout();

  122. QHBoxLayout *UserSelectHBoxLayout = new QHBoxLayout();

  123. QVBoxLayout *UserSelectVBoxLayout = new QVBoxLayout();

  124.  
  125. /*****************界面右侧group布局******************/

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

  127. {

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

  129. }

  130. UserSelectHBoxLayout->addWidget(&m_OKButton);

  131. UserSelectHBoxLayout->addWidget(&m_CancelButton);

  132.  
  133. UserSelectVBoxLayout->addLayout(UserSelectFormLayout);

  134. UserSelectVBoxLayout->addLayout(UserSelectHBoxLayout);

  135. UserSelectVBoxLayout->addStretch();

  136.  
  137. /*****************界面右侧group布局 END******************/

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

  140.  
  141. UserEditHBoxLayout->addLayout(UserPrimaryKeyFormLayout);

  142. UserEditHBoxLayout->addWidget(&m_QueryButton);

  143. UserEditHBoxLayout->addStretch();

  144.  
  145. UserButtonHBoxLayout->addWidget(&m_NewButton);

  146. UserButtonHBoxLayout->addWidget(&m_UpdateButton);

  147. UserButtonHBoxLayout->addWidget(&m_DeleteButton);

  148.  
  149. UserGroupVBoxLayout->addLayout(UserEditHBoxLayout);

  150. UserGroupVBoxLayout->addLayout(UserButtonHBoxLayout);

  151.  
  152. m_Group.setLayout(UserGroupVBoxLayout);

  153.  
  154. TotalVBoxLayout->addWidget(&m_Group);

  155. TotalVBoxLayout->addWidget(m_TabView);

  156.  
  157. TotalHBoxLayout->addLayout(TotalVBoxLayout, 3);

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

  159.  
  160. setLayout(TotalHBoxLayout);

  161. }

  162.  
  163. /**************************************************************************

  164. * 函数名称:onNewUserButtonClick

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

  166. * 输入参数:无

  167. * 输出参数:无

  168. * 返回数值:void

  169. * 创建人员:

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

  171. * 修改人员:

  172. * 修改时间:

  173. **************************************************************************/

  174. void KSDemoDlg::onNewButtonClicked()

  175. {

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

  177. {

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

  179. }

  180. m_operator = INSERT;

  181. m_OKButton.setEnabled(true);

  182. m_CancelButton.setEnabled(true);

  183. }

  184.  
  185. /**************************************************************************

  186. * 函数名称:onQueryUserButtonClick

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

  188. * 输入参数:无

  189. * 输出参数:无

  190. * 返回数值:void

  191. * 创建人员:廖明胜

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

  193. * 修改人员:

  194. * 修改时间:

  195. **************************************************************************/

  196. void KSDemoDlg::onQueryButtonClicked()

  197. {

  198. QString toFind = m_PrimaryKeyLineEdit.text();

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

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

  203.  
  204. m_model->select();

  205. }

  206.  
  207. /**************************************************************************

  208. * 函数名称:onUpdateButtonClicked

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

  210. * 输入参数:无

  211. * 输出参数:无

  212. * 返回数值:void

  213. * 创建人员:

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

  215. * 修改人员:

  216. * 修改时间:

  217. **************************************************************************/

  218. void KSDemoDlg::onUpdateButtonClicked()

  219. {

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

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

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

  225. {

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

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

  228. }

  229. m_operator = UPDATE;

  230. m_OKButton.setEnabled(true);

  231. m_CancelButton.setEnabled(true);

  232.  
  233. }

  234.  
  235. /**************************************************************************

  236. * 函数名称:onDeleteButtonClicked

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

  238. * 输入参数:无

  239. * 输出参数:无

  240. * 返回数值:void

  241. * 创建人员:

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

  243. * 修改人员:

  244. * 修改时间:

  245. **************************************************************************/

  246. void KSDemoDlg::onDeleteButtonClicked()

  247. {

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

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

  251. {

  252. m_model->removeRow(toDelRow);

  253. m_model->submitAll();

  254. }

  255.  
  256. m_model->select();

  257. }

  258.  
  259. /**************************************************************************

  260. * 函数名称:onUserNameEditEmpty

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

  262. * 输入参数:无

  263. * 输出参数:无

  264. * 返回数值:void

  265. * 创建人员:

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

  267. * 修改人员:

  268. * 修改时间:

  269. **************************************************************************/

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

  271. {

  272. if (text.isEmpty())

  273. {

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

  275. m_model->select();

  276. }

  277. }

  278.  
  279. /**************************************************************************

  280. * 函数名称:onCurrentTableViewActived

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

  282. * 输入参数:无

  283. * 输出参数:无

  284. * 返回数值:void

  285. * 创建人员:

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

  287. * 修改人员:

  288. * 修改时间:

  289. **************************************************************************/

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

  291. {

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

  293. {

  294. return;

  295. }

  296.  
  297. int currentRow = index.row();

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

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

  302. {

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

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

  305. }

  306. }

  307.  
  308. /**************************************************************************

  309. * 函数名称:onOKButtonClicked

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

  311. * 输入参数:无

  312. * 输出参数:无

  313. * 返回数值:void

  314. * 创建人员:

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

  316. * 修改人员:

  317. * 修改时间:

  318. **************************************************************************/

  319. void KSDemoDlg::onOKButtonClicked()

  320. {

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

  322. {

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

  324. {

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

  326. return;

  327. }

  328. }

  329.  
  330. switch (m_operator)

  331. {

  332. case INSERT:

  333. {

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

  335. {

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

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

  338. m_model->insertRow(row);

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

  340. {

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

  342. }

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

  345. }

  346. }

  347. break;

  348. case UPDATE:

  349. {

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

  351. {

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

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

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

  355. {

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

  357. }

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

  360. }

  361. }

  362. break;

  363. default:

  364. break;

  365. }

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

  368. {

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

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

  371. }

  372.  
  373. m_model->select();

  374. m_OKButton.setEnabled(false);

  375. m_CancelButton.setEnabled(false);

  376. }

  377.  
  378. /**************************************************************************

  379. * 函数名称:onCancelButtonClicked

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

  381. * 输入参数:无

  382. * 输出参数:无

  383. * 返回数值:void

  384. * 创建人员:

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

  386. * 修改人员:

  387. * 修改时间:

  388. **************************************************************************/

  389. void KSDemoDlg::onCancelButtonClicked()

  390. {

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

  392. {

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

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

  395. }

  396. m_OKButton.setEnabled(false);

  397. m_CancelButton.setEnabled(false);

  398. }

  399.  
  400. /**************************************************************************

  401. * 函数名称:~KsUserManageDlg

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

  403. * 输入参数:无

  404. * 输出参数:无

  405. * 返回数值:void

  406. * 创建人员:

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

  408. * 修改人员:

  409. * 修改时间:

  410. **************************************************************************/

  411.  
  412. KSDemoDlg::~KSDemoDlg()

  413. {

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

  415. m_db.close();

  416. }

  417.  

main函数
 

 
  1. #include "KsTestDemo.h"

  2. #include <QtWidgets/QApplication>

  3. #include <QCoreApplication>

  4.  
  5. #include "KSDemoDlg.h"

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

  8. {

  9. QApplication a(argc, argv);

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

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

  14. return a.exec();

  15. }

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

Qt作为一个跨平台的应用开发工具,提供了丰富的API和类库,可以方便地操作多个SQLite数据库和文件读写。下面是一些常见的Qt操作多个SQLite数据库和文件读写的方法: 1. 使用QSqlDatabase类:Qt提供了QSqlDatabase类来连接和管理SQLite数据库。可以使用QSqlDatabase::addDatabase()方法创建连接对象,然后使用QSqlDatabase::setDatabaseName()方法指定数据库名称和路径。如果需要同时连接多个数据库,可以为每个数据库创建独立的QSqlDatabase对象,在需要时打开和关闭数据库连接。 2. 使用QSqlQuery类:QSqlQuery类可以执行SQL查询和更新语句,并获取查询结果。连接到不同的数据库时,只需使用不同的QSqlDatabase对象创建QSqlQuery对象即可。 3. 使用QFile类:Qt提供了QFile类来读写文件。可以使用QFile::open()方法打开文件,使用QFile::read()或QFile::write()方法读写文件内容。如果需要同时读写多个文件,可以为每个文件创建独立的QFile对象。 4. 使用QFileSystemModel类:QFileSystemModel类提供了访问文件系统的API。可以使用QFileSystemModel::setRootPath()方法指定要访问的文件夹路径,然后使用QFileSystemModel::index()方法获取文件和文件夹的索引,使用QFileSystemModel::data()方法获取文件属性和内容。 总之,Qt提供了丰富的工具和类库来操作多个SQLite数据库和文件读写,可以根据需要选择合适的方法来实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值