• qt简易网络聊天室 数据库的练习


    qt网络聊天室

    服务器:

    配置文件.pro

    1. QT += core gui network
    2. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    3. CONFIG += c++11
    4. # The following define makes your compiler emit warnings if you use
    5. # any Qt feature that has been marked deprecated (the exact warnings
    6. # depend on your compiler). Please consult the documentation of the
    7. # deprecated API in order to know how to port your code away from it.
    8. DEFINES += QT_DEPRECATED_WARNINGS
    9. # You can also make your code fail to compile if it uses deprecated APIs.
    10. # In order to do so, uncomment the following line.
    11. # You can also select to disable deprecated APIs only up to a certain version of Qt.
    12. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
    13. SOURCES += \
    14. main.cpp \
    15. widget.cpp
    16. HEADERS += \
    17. widget.h
    18. FORMS += \
    19. widget.ui
    20. # Default rules for deployment.
    21. qnx: target.path = /tmp/$${TARGET}/bin
    22. else: unix:!android: target.path = /opt/$${TARGET}/bin
    23. !isEmpty(target.path): INSTALLS += target

    头文件

    widget.h

    1. #ifndef WIDGET_H
    2. #define WIDGET_H
    3. #include
    4. #include //服务器类头文件
    5. #include //信息调试类
    6. #include //消息对话框类头文件
    7. #include //套接字类头文件
    8. QT_BEGIN_NAMESPACE
    9. namespace Ui { class Widget; }
    10. QT_END_NAMESPACE
    11. class Widget : public QWidget
    12. {
    13. Q_OBJECT
    14. public:
    15. Widget(QWidget *parent = nullptr);
    16. ~Widget();
    17. private slots:
    18. void on_startBtn_clicked();
    19. void newConnection_slot(); //自定义处理newConnection信号的函数
    20. void readyRead_slot(); //自定义处理readyRead信号的槽函数
    21. private:
    22. Ui::Widget *ui;
    23. //定义一个服务器指针
    24. QTcpServer *server;
    25. //定义一个客户端容器
    26. QList clientList;
    27. };
    28. #endif // WIDGET_H

    源文件

    main.cpp

    1. #include "widget.h"
    2. #include
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication a(argc, argv);
    6. Widget w;
    7. w.show();
    8. return a.exec();
    9. }

    widget.cpp

    1. //服务器
    2. //1、实例化服务器类对象,该对象就是服务器
    3. //2、服务器调用成员函数listen()将服务器设置为被动监听状态,监听时可以指定主机地址,也可以any监听所有的主机地址。 端口号若为0,则服务器自动分配,也可指定端口号
    4. //3、若有客户端连接,则服务器会发送一个newconnection信号,连接到对应的槽函数中处理相关逻辑
    5. //4、在处理connect信号的槽函数中,使用nextPendingConntiin获取最新连接的客户端套接字
    6. //5、将获取的套接字存入客户端容器中
    7. //6、若客户端有数据项向服务器发送,则客户端套接字会自动发射一个readyRead信号,来凝结到对应的槽函数中处理相关逻辑
    8. //7、在处理readyRead信号的槽函数中,来读取数据,
    9. //7-1、先循环遍历哪些套接字是无效的(断开连接的客户端),将其从客户端容器中删除,使用state函数判断状态,为0则是断开连接的,不为0不是断开的
    10. //7-2、在循环遍历判断是哪个客户端发来的数据,使用BytesAviable来判断哪个客户端有数据,为0则代表无
    11. //7-3、若有数据,使用read readLine readAll函数读取数据,
    12. //11、向套接字中写数据,可以使用write函数
    13. #include "widget.h"
    14. #include "ui_widget.h"
    15. Widget::Widget(QWidget *parent)
    16. : QWidget(parent)
    17. , ui(new Ui::Widget)
    18. {
    19. ui->setupUi(this);
    20. //给服务器实例化对象
    21. server = new QTcpServer(this); //该对象就是服务器,服务器创建完成
    22. }
    23. Widget::~Widget()
    24. {
    25. delete ui;
    26. }
    27. //启动服务器的槽函数
    28. void Widget::on_startBtn_clicked()
    29. {
    30. if(ui->portEdit->text().isEmpty())
    31. {
    32. QMessageBox::critical(this, "失败", "端口号不可为空");
    33. return;
    34. }
    35. //调用成员函数listen()将服务器设置为被动监听状态
    36. //函数原型:bool listen(const QHostAddress &address = QHostAddress::Any, quint16 port = 0);
    37. //参数1:主机号,any监听所有的主机地址, 指定主机地址,监听指定的主机地址。
    38. //参数2:通过哪个端口号访问服务器,若为0,则服务器自由分配,若不为0,则指定要监听的端口号, quint16 是无符号的16位整型, short
    39. //返回值,成功返回真,失败返回假
    40. //获取端口号
    41. quint16 port = ui->portEdit->text().toUInt(); //toUInt(), 转为无符号整型
    42. if(!server->listen(QHostAddress::Any, port))
    43. {
    44. QMessageBox::critical(this, "失败", "服务器启动失败");
    45. return;
    46. }
    47. else
    48. {
    49. QMessageBox::information(this, "成功", "服务器启动成功");
    50. }
    51. //此时说明服务器启动成功,并对客户端进行监听
    52. //若有客户端发送连接请求,则服务器会自动发射一个newconnection信号
    53. //将给信号连接到对应的槽函数中
    54. connect(server, &QTcpServer::newConnection, this, &Widget::newConnection_slot);
    55. }
    56. //处理newConnection函数的槽函数的实现
    57. void Widget::newConnection_slot()
    58. {
    59. //循环遍历,从客户端套接字容器中删去已断开连接的客户端套接字
    60. //函数原型:SocketState state() const;
    61. //功能:判断套接字的状态
    62. //参数,无
    63. //返回值:返回套接字的状态(枚举, 0代表断开连接)
    64. for(int i = 0; icount(); i++)
    65. {
    66. if(0 == clientList.at(i)->state())
    67. {
    68. //移除下标为i的无效的客户端
    69. clientList.removeAt(i);
    70. }
    71. }
    72. //获取新连接的客户端的套接字
    73. //函数原型:virtual QTcpSocket *nextPendingConnection();
    74. //功能:获取新连接的客户端的套接字
    75. //无参,返回值就是获取到的客户端的套接字的指针
    76. QTcpSocket *s = server->nextPendingConnection();
    77. //将获取到的套接字存入到客户端套接字容器中
    78. clientList.push_back(s);
    79. //此时客户端和服务器建立连接
    80. //如果套接字有数据项发送给服务器时,该套接字自动发射一个readyRead信号
    81. //将readyRead信号连接到对应的槽函数中处理相关逻辑
    82. connect(s, &QTcpSocket::readyRead, this, &Widget::readyRead_slot);
    83. }
    84. //自定义处理readyRead信号的槽函数的实现
    85. void Widget::readyRead_slot()
    86. {
    87. //遍历判断是哪个客户端套发来的数据
    88. //使用BytesAvalible函数,若无数据可读则为0
    89. for(int i=0; icount(); i++)
    90. {
    91. if(clientList[i]->bytesAvailable() != 0)
    92. {
    93. //读取套接字中的数据,可用read/readall/readline
    94. //函数原型QByteArray readAll();
    95. //功能:读取数据
    96. //无参,返回值,c风格的字符串
    97. QByteArray msg = clientList[i]->readAll();
    98. //将数据展示到ui界面上
    99. ui->msgList->addItem(QString::fromLocal8Bit(msg)); //将C风格的字符串转为QString类型的
    100. //将接收到的消息发送给所有的客户端
    101. for(int j=0; jcount(); j++)
    102. {
    103. clientList[j]->write(msg);
    104. }
    105. }
    106. }
    107. }

    ui界面

    客户端

    配置文件.pro

    1. QT += core gui network
    2. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    3. CONFIG += c++11
    4. # The following define makes your compiler emit warnings if you use
    5. # any Qt feature that has been marked deprecated (the exact warnings
    6. # depend on your compiler). Please consult the documentation of the
    7. # deprecated API in order to know how to port your code away from it.
    8. DEFINES += QT_DEPRECATED_WARNINGS
    9. # You can also make your code fail to compile if it uses deprecated APIs.
    10. # In order to do so, uncomment the following line.
    11. # You can also select to disable deprecated APIs only up to a certain version of Qt.
    12. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
    13. SOURCES += \
    14. main.cpp \
    15. widget.cpp
    16. HEADERS += \
    17. widget.h
    18. FORMS += \
    19. widget.ui
    20. # Default rules for deployment.
    21. qnx: target.path = /tmp/$${TARGET}/bin
    22. else: unix:!android: target.path = /opt/$${TARGET}/bin
    23. !isEmpty(target.path): INSTALLS += target

    头文件

    1. #ifndef WIDGET_H
    2. #define WIDGET_H
    3. #include
    4. #include //客户端头文件
    5. #include //信息调试类头文件
    6. #include //消息对话框类头文件
    7. QT_BEGIN_NAMESPACE
    8. namespace Ui { class Widget; }
    9. QT_END_NAMESPACE
    10. class Widget : public QWidget
    11. {
    12. Q_OBJECT
    13. public:
    14. Widget(QWidget *parent = nullptr);
    15. ~Widget();
    16. private slots:
    17. void on_connectBtn_clicked();
    18. void connected_slot(); //自定义处理connected信号的槽函数
    19. void readyRead_slot(); //自定义处理readyRead信号的槽函数
    20. void on_sendBtn_clicked();
    21. void on_disConnectBtn_clicked();
    22. void disconnected_slot(); //自定义处理disconnected信号的槽函数
    23. private:
    24. Ui::Widget *ui;
    25. //定义一个客户端指针
    26. QTcpSocket *client;
    27. };
    28. #endif // WIDGET_H

    源文件

    main.cpp

    1. #include "widget.h"
    2. #include
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication a(argc, argv);
    6. Widget w;
    7. w.show();
    8. return a.exec();
    9. }

    widget.cpp

    1. //客户端
    2. //1、实例化QTcpSocket类对象,该对象就是客户端
    3. //2、调用客户端对象的connecTtoHost的成员函数向服务器发送连接请求,需要给定服务器的ip和端口
    4. //3、如果与服务器连接成功,客户端自动发射一个connected信号,将信号与对应的槽函数进行连接处理相关逻辑即可
    5. //4、如果服务器有数据项向客户端发送,客户端会自动发射一个readyRead信号,将信号连接到对应的槽函数读取数据
    6. //5、若客户端要给服务器发送消息,使用write函数
    7. //6、在处理readyRead信号的槽函数中,使用read/readLine/readAll函数读取数据
    8. //7、使用该客户端对象的disconnectFromHost函数端口与服务器的连接,
    9. //8、如果成功断开则会自动发射disconnected信号,将disconnected信号与对应的槽函数连接处理即可
    10. #include "widget.h"
    11. #include "ui_widget.h"
    12. Widget::Widget(QWidget *parent)
    13. : QWidget(parent)
    14. , ui(new Ui::Widget)
    15. {
    16. ui->setupUi(this);
    17. //实例化客户端空间
    18. client = new QTcpSocket(this); //该对象就是客户端,创建客户端成功
    19. ui->sendBtn->setEnabled(false);
    20. ui->disConnectBtn->setEnabled(false);
    21. //若连接成功,则客户端自动发射一个connected信号
    22. //将connected信号连接到对应的槽函数处理相关逻辑
    23. //因为只需要连接一次,所以将其写到构造函数中
    24. connect(client, &QTcpSocket::connected, this, &Widget::connected_slot);
    25. //若服务器有数据项向客户端发送,客户端会自动发送readyRead信号
    26. //将readyRead信号与对应的槽函数连接
    27. //由于只需连接一次,所以写在构造函数中
    28. connect(client, &QTcpSocket::readyRead, this, &Widget::readyRead_slot);
    29. //若断开成功,则客户端会自动发射一个disconnected信号
    30. //连接到对应的槽函数处理即可
    31. //因只需要连接一次,写在构造函数即可
    32. connect(client, &QTcpSocket::disconnected, this, &Widget::disconnected_slot);
    33. }
    34. Widget::~Widget()
    35. {
    36. delete ui;
    37. }
    38. //连接服务器按钮的槽函数
    39. void Widget::on_connectBtn_clicked()
    40. {
    41. if(ui->userNameEdit->text().isEmpty() || ui->ipEdit->text().isEmpty() || ui->portEdit->text().isEmpty())
    42. {
    43. QMessageBox::critical(this, "Error", "信息填写不完整");
    44. return;
    45. }
    46. //获取服务器的主机地址
    47. QString ip = ui->ipEdit->text();
    48. //获取服务器的端口号
    49. quint16 port = ui->portEdit->text().toUInt(); //toUInt()转化为无符号整数
    50. //该客户端对象调用成员函数连接服务器
    51. //函数原型:virtual void connectToHost(const QHostAddress &address, quint16 port, OpenMode mode = ReadWrite);
    52. //功能:客户端连接服务器
    53. //参数1:服务器的ip地址
    54. //参数2:客户端的端口号 , quint16 无符号的16位的整型 short
    55. //返回值:无
    56. client->connectToHost(ip, port);
    57. //顺便向客户端发送一条数据
    58. QString msg = ui->userNameEdit->text();
    59. msg = msg+ ": 进入聊天室";
    60. //使用write函数向服务器发送数据
    61. client->write(msg.toLocal8Bit()); // toLocal8Bit()转为C风格字符串
    62. //若连接成功,则客户端自动发射一个connected信号
    63. //将connected信号连接到对应的槽函数处理相关逻辑
    64. //因为只需要连接一次,所以将其写到构造函数中
    65. }
    66. //处理connected信号的槽函数的实现
    67. void Widget::connected_slot()
    68. {
    69. //说明连接成功
    70. ui->userNameEdit->setEnabled(false);
    71. ui->ipEdit->setEnabled(false);
    72. ui->portEdit->setEnabled(false);
    73. ui->connectBtn->setEnabled(false);
    74. ui->disConnectBtn->setEnabled(true);
    75. ui->sendBtn->setEnabled(true);
    76. //若服务器有数据项向客户端发送,客户端会自动发送readyRead信号
    77. //将readyRead信号与对应的槽函数连接
    78. //由于只需连接一次,所以写在构造函数中
    79. }
    80. //处理readyRead信号的槽函数
    81. void Widget::readyRead_slot()
    82. {
    83. //使用read/readLine/readAll读取数据
    84. QByteArray msg =client->readAll();
    85. //将读取的数据展示到ui界面上
    86. ui->msgList->addItem(QString::fromLocal8Bit(msg));
    87. }
    88. //发送按钮的槽函数
    89. void Widget::on_sendBtn_clicked()
    90. {
    91. //获取用户名
    92. QString userName = ui->userNameEdit->text();
    93. //获取文本输入框的内容
    94. QString msg = ui->textEdit->toPlainText();
    95. msg = userName + ": " + msg;
    96. //将数据发送给服务器
    97. client->write(msg.toLocal8Bit());
    98. //清空文本输入框
    99. ui->textEdit->clear();
    100. }
    101. //断开按钮对应的槽函数
    102. void Widget::on_disConnectBtn_clicked()
    103. {
    104. //告知服务器即将断开连接
    105. QString userName = ui->userNameEdit->text();
    106. QString msg = userName + ": 退出聊天室";
    107. client->write(msg.toLocal8Bit());
    108. //使用该类对象调用disconnectFromHost断开与服务器的连接
    109. //函数原型:virtual void disconnectFromHost();
    110. //无参 无返
    111. client->disconnectFromHost();
    112. //若断开成功,则客户端会自动发射一个disconnected信号
    113. //连接到对应的槽函数处理即可
    114. //因只需要连接一次,写在构造函数即可
    115. }
    116. void Widget::disconnected_slot()
    117. {
    118. ui->sendBtn->setEnabled(false);
    119. ui->disConnectBtn->setEnabled(false);
    120. ui->userNameEdit->setEnabled(true);
    121. ui->ipEdit->setEnabled(true);
    122. ui->portEdit->setEnabled(true);
    123. QMessageBox::information(this, "退出", "断开成功");
    124. }

    ui界面

    数据库:
    配置文件.pro

    1. QT += core gui sql
    2. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    3. CONFIG += c++11
    4. # The following define makes your compiler emit warnings if you use
    5. # any Qt feature that has been marked deprecated (the exact warnings
    6. # depend on your compiler). Please consult the documentation of the
    7. # deprecated API in order to know how to port your code away from it.
    8. DEFINES += QT_DEPRECATED_WARNINGS
    9. # You can also make your code fail to compile if it uses deprecated APIs.
    10. # In order to do so, uncomment the following line.
    11. # You can also select to disable deprecated APIs only up to a certain version of Qt.
    12. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
    13. SOURCES += \
    14. main.cpp \
    15. widget.cpp
    16. HEADERS += \
    17. widget.h
    18. FORMS += \
    19. widget.ui
    20. # Default rules for deployment.
    21. qnx: target.path = /tmp/$${TARGET}/bin
    22. else: unix:!android: target.path = /opt/$${TARGET}/bin
    23. !isEmpty(target.path): INSTALLS += target

    头文件

    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_inputBtn_clicked();
    20. void on_showBtn_clicked();
    21. void on_searchBtn_clicked();
    22. void on_deleteBtn_clicked();
    23. private:
    24. Ui::Widget *ui;
    25. //定义一个数据库对象
    26. QSqlDatabase db;
    27. };
    28. #endif // WIDGET_H

    源文件:

    main.cpp

    1. #include "widget.h"
    2. #include
    3. int main(int argc, char *argv[])
    4. {
    5. QApplication a(argc, argv);
    6. Widget w;
    7. w.show();
    8. return a.exec();
    9. }

    widget.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. //判断数据库对象是否包含了自己使用的数据库,Student.db
    9. if(!db.contains("Student.db"))
    10. {
    11. //添加数据库
    12. // static QSqlDatabase addDatabase(QSqlDriver* driver, const QString& connectionName = QLatin1String(defaultConnection));
    13. //参数1:数据库的版本
    14. //返回值:添加的数据库
    15. db = QSqlDatabase::addDatabase("QSQLITE"); //表明使用的是Sqlite3版本的数据库
    16. //给数据库命名
    17. db.setDatabaseName("Student.db");
    18. }
    19. //打开数据库
    20. if(!db.open())
    21. {
    22. QMessageBox::information(this, "提示", "数据库打开失败");
    23. }
    24. //此时说明数据库已经创建出来并打开了,就可以创建数据表了
    25. //创建数据表需要使用sql语句, 需要使用QSQLQuerry对象来完成
    26. //准备sql语句
    27. // QString sql = "create table if not exists myTable("
    28. // "id integer Primary key autoincrement,"
    29. // "numb integer,"
    30. // "name varchar(10),"
    31. // "sex varchar(4))";
    32. QString sql = "create table if not exists myTable(" //创建表的sql语句
    33. "id integer primary key autoincrement," //id主键,允许自增
    34. "numb integer," //学号,是整形
    35. "name varchar(10)," //姓名 字符串
    36. "score integer,"
    37. "sex varchar(4))"; //性别 字符串
    38. //定义语句执行者
    39. QSqlQuery queery;
    40. //使用querry调用成员函数exec执行sql语句
    41. if(!queery.exec(sql))
    42. {
    43. QMessageBox::information(this, "失败", "表格创建失败");
    44. return;
    45. }
    46. else
    47. {
    48. QMessageBox::information(this, "成功", "表格创建出成功");
    49. }
    50. }
    51. Widget::~Widget()
    52. {
    53. delete ui;
    54. }
    55. //录入按钮对应的槽函数
    56. void Widget::on_inputBtn_clicked()
    57. {
    58. //获取ui界面中要存入数据库中的数据
    59. int numb_ui = ui->numEdit->text().toUInt(); //获取ui界面的学号
    60. QString name_ui = ui->nameEdit_2->text(); //获取ui界面的姓名
    61. int score_ui = ui->scoreEdit_3->text().toUInt(); //获取ui界面上的成绩
    62. QString sex_ui = ui->sexEdit_4->text(); //获取ui界面上的性别
    63. //判断是否有漏填数据
    64. if(numb_ui == 0 || name_ui.isEmpty() || score_ui == 0 || sex_ui.isEmpty())
    65. {
    66. QMessageBox::information(this, "提示", "请将数据填写完整");
    67. return;
    68. }
    69. //准备sql语句
    70. QString sql = QString("insert into myTable(numb, name, score, sex)"
    71. "values(%1, '%2', %3, '%4')")
    72. .arg(numb_ui).arg(name_ui).arg(score_ui).arg(sex_ui);
    73. qDebug() << sql;
    74. //定义语句执行者
    75. QSqlQuery querry;
    76. //调用执行者的exec()函数执行sql语句
    77. if(!querry.exec(sql))
    78. {
    79. QMessageBox::information(this, "失败", "数据插入失败");
    80. }
    81. else
    82. {
    83. QMessageBox::information(this, "成功", "数据录入成功");
    84. }
    85. }
    86. //展示按钮对应的槽函数
    87. void Widget::on_showBtn_clicked()
    88. {
    89. QString sql = "select * from MyTable";
    90. //定义语句执行者
    91. QSqlQuery querry;
    92. //调用执行者的exec()函数执行sql语句
    93. if(!querry.exec(sql))
    94. {
    95. QMessageBox::information(this, "失败", "查询失败");
    96. return;
    97. }
    98. else
    99. {
    100. QMessageBox::information(this, "成功", "查询成功");
    101. }
    102. //此时,将查找到的所有结果。全部都放在querry中了
    103. //可以通过next函数不断遍历查询结果
    104. int i = 0; //记录行号
    105. while(querry.next())
    106. {
    107. //遍历的就是任意一组记录querry.record
    108. //qDebug() << querry.record();
    109. //querry.record().count(); 返回当前记录对应数据项的个数
    110. //querry.record().count();
    111. //要找到每条记录中的每个数据
    112. //qDebug() << querry.record().value(2);
    113. //querry.record().value(j).toString() //将记录的某一项的转为字符串
    114. //将数据库中的表格展示到ui界面
    115. for(int j = 0; jrecord().count()-1; j++)
    116. {
    117. ui->tableWidget->setItem(i, j, new QTableWidgetItem(querry.record().value(j+1).toString()));
    118. }
    119. i++; //进行下一行
    120. }
    121. }
    122. void Widget::on_searchBtn_clicked()
    123. {
    124. //获取学号
    125. QString name_ui = ui->nameEdit_2->text();
    126. int numb_ui = ui->numEdit->text().toUInt();
    127. //sql语句
    128. QString sql = QString("select * from myTable where numb = %1").arg(numb_ui);
    129. //定义语句执行者
    130. QSqlQuery querry;
    131. //执行者使用exec函数执行sql语句
    132. if(!querry.exec(sql))
    133. {
    134. QMessageBox::information(this , "提示", "查询失败");
    135. return;
    136. }
    137. else
    138. {
    139. QMessageBox::information(this , "提示", "查询成功");
    140. }
    141. int i = 0; //行号
    142. while(querry.next())
    143. {
    144. for(int j=0; jrecord().count()-1; j++)
    145. {
    146. ui->tableWidget->setItem(i, j, new QTableWidgetItem(querry.record().value(j+1).toString()));
    147. }
    148. }
    149. }
    150. void Widget::on_deleteBtn_clicked()
    151. {
    152. QString name_ui = ui->nameEdit_2->text();
    153. //sql语句
    154. QString sql = QString("delete from myTable where name = '%1'").arg(name_ui);
    155. qDebug() << sql;
    156. //定义语句执行者
    157. QSqlQuery querry;
    158. //querry调用自己成员函数执行sql语句
    159. if(!querry.exec(sql))
    160. {
    161. QMessageBox::information(this, "提示", "删除失败");
    162. return;
    163. }
    164. else
    165. {
    166. QMessageBox::information(this, "提示", "删除成功");
    167. }
    168. }

    ui界面

  • 相关阅读:
    springboot+nodejs+vue+Elementui在线旅游管理系统
    SpringBoot项目整合MybatisPlus持久层框架+Druid数据库连接池
    主成分分析(PCA)介绍
    功能定义-变道碰撞预警
    docker for windows 转移存储位置
    什么是Air-gapped test or development environments?
    可以将 CSS 模块用于预处理器吗?
    提升办公效率,畅享多功能办公笔记软件Notion for Mac
    客快物流大数据项目(七十):Impala入门介绍
    html 高性能 简易轮播图
  • 原文地址:https://blog.csdn.net/qq_46766479/article/details/132679582