• QT_day6


    qt与数据库连接,实现添加、显示、查找、删除功能

    weiget.h

    1. #ifndef WIDGET_H
    2. #define WIDGET_H
    3. #include
    4. #include //数据库管理 类
    5. #include //执行sql语句 类
    6. #include //数据库记录 类
    7. #include //数据库错误 类
    8. #include
    9. QT_BEGIN_NAMESPACE
    10. namespace Ui { class Widget; }
    11. QT_END_NAMESPACE
    12. class Widget : public QWidget
    13. {
    14. Q_OBJECT
    15. public:
    16. Widget(QWidget *parent = nullptr);
    17. ~Widget();
    18. private slots:
    19. void on_addBtn_clicked();
    20. void on_showBtn_clicked();
    21. void on_selectBtn_clicked();
    22. void on_deleteBtn_clicked();
    23. private:
    24. Ui::Widget *ui;
    25. //实例化一个数据库对象
    26. QSqlDatabase db;
    27. };
    28. #endif // WIDGET_H

    weiget.cpp

    1. #include "widget.h"
    2. #include "ui_widget.h"
    3. Widget::Widget(QWidget *parent)
    4. : QWidget(parent)
    5. , ui(new Ui::Widget)
    6. {
    7. ui->setupUi(this);
    8. //判断数据库是否存在
    9. if(!db.contains())
    10. {
    11. //说明数据库不存在
    12. //创建一个数据库
    13. db = QSqlDatabase::addDatabase("QSQLITE"); //表示数据库驱动为sqlite3
    14. //给创建的数据库命名
    15. db.setDatabaseName("stuInfo.db");
    16. QMessageBox::information(this,"","创建数据库成功");
    17. }
    18. //打开数据库
    19. if(!db.open())
    20. {
    21. QMessageBox::information(this,"","打开数据库失败");
    22. return;
    23. }
    24. //给数据库创建数据库表
    25. //实例化一个执行sql语句的类对象
    26. QSqlQuery query;
    27. //准备sql语句
    28. QString sql = "create table if not exists stu_info_table("
    29. "id integer primary key autoincrement,"
    30. "numb integer,"
    31. "name varchar(20),"
    32. "sex varchar(4),"
    33. "score interger)";
    34. //执行sql语句
    35. if(query.exec(sql))
    36. {
    37. QMessageBox::information(this,"","创建数据库表成功");
    38. }
    39. else
    40. {
    41. QMessageBox::information(this,"","创建数据库表失败");
    42. }
    43. }
    44. Widget::~Widget()
    45. {
    46. delete ui;
    47. }
    48. //添加按钮对应的槽函数
    49. void Widget::on_addBtn_clicked()
    50. {
    51. //获取ui界面上的信息
    52. int numb = ui->numEdit->text().toInt(); //将学号文本转换成整型
    53. QString name = ui->nameEdit->text();
    54. QString sex = ui->sexEdit->text();
    55. int score = ui->scoreEdit->text().toUInt(); //将成绩文本转换成整型
    56. //保证用户输入完整信息
    57. if(numb == 0 || name.isEmpty() || sex.isEmpty() || score == 0)
    58. {
    59. QMessageBox::information(this,"","请将信息填写完整");
    60. return;
    61. }
    62. //将信息加入到数据库中
    63. //需要实例化一个执行sql语句的类对象
    64. QSqlQuery query;
    65. //准备sql语句
    66. QString sql = QString("insert into stu_info_table(numb,name,sex,score)"
    67. "values(%1,'%2','%3',%4)")
    68. .arg(numb).arg(name).arg(score);
    69. //执行sql语句
    70. if(query.exec(sql))
    71. {
    72. QMessageBox::information(this,"","添加成功");
    73. }
    74. else
    75. {
    76. QMessageBox::information(this,"","添加失败");
    77. }
    78. }
    79. //显示按钮对应的槽函数
    80. void Widget::on_showBtn_clicked()
    81. {
    82. //实例化一个执行sql语句的类对象
    83. QSqlQuery query;
    84. //准备sql语句
    85. QString sql = "select * from stu_info_table";
    86. //执行sql语句
    87. if(query.exec(sql))
    88. {
    89. //将数据库里的内容放入ui界面上
    90. int i = 0; //记录行号
    91. while(query.next()) //遍历查询结果
    92. {
    93. for(int j = 0; jrecord().count(); j++)
    94. {
    95. ui->tableWidget->setItem(i,j,new QTableWidgetItem(query.value(j+1).toString()));
    96. }
    97. i++;
    98. }
    99. }
    100. }
    101. //查找按钮对应的槽函数
    102. void Widget::on_selectBtn_clicked()
    103. {
    104. //实例化一个执行sql语句的类对象
    105. QSqlQuery query;
    106. //准备sql语句
    107. int numb = ui->numEdit->text().toInt();
    108. QString sql = QString("select * from stu_info_table where numb = %1").arg(numb);
    109. //执行sql语句
    110. if(query.exec(sql))
    111. {
    112. Widget::on_showBtn_clicked();
    113. QMessageBox::information(this,"","查找成功");
    114. }
    115. else
    116. {
    117. Widget::on_showBtn_clicked();
    118. QMessageBox::information(this,"","查找失败");
    119. }
    120. }
    121. //删除按钮对应的槽函数
    122. void Widget::on_deleteBtn_clicked()
    123. {
    124. //实例化一个执行sql语句的类对象
    125. QSqlQuery query;
    126. //准备sql语句
    127. int numb = ui->numEdit->text().toInt();
    128. QString sql = QString("delete from stu_info_table where numb = %1").arg(numb);
    129. //执行sql语句
    130. if(query.exec(sql))
    131. {
    132. Widget::on_showBtn_clicked();
    133. QMessageBox::information(this,"","删除成功");
    134. }
    135. else
    136. {
    137. Widget::on_showBtn_clicked();
    138. QMessageBox::information(this,"","删除失败");
    139. }
    140. }

  • 相关阅读:
    4位资深专家多年大厂经验分享出Flink技术架构设计与实现原理
    elastic search添加密码验证、并且使用postman访问带密码的es
    汇率之谜:揭秘黄金折算与真实人民币汇率的神秘差距
    linux创建ftp账号
    人工智能的隐私保护探讨
    多模态论文串讲
    MySQL主键使用数值型和字符型的区别
    Java毕业设计-音乐管理系统
    RecAgent:A Novel Simulation Paradigm for Recommender Systems学习笔记
    ObjectARX如何锁定一个图层
  • 原文地址:https://blog.csdn.net/2301_79218296/article/details/134001663