本文实了数据库的增删除改查询功能
#ifndef WIDGET_H
#define WIDGET_H
#include <QtSql/QSql>
#include <QWidget>
#include<QSqlDatabase>
#include <QtSql/qsqlquery.h>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
struct student
{
QString Name;
int Age;
QString Class;
student(QString name,int age ,QString classs) {
this->Name=name;
this->Age=age;
this->Class = classs;
}
};
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
private:
bool try_connect_database();
void update_value(QSqlDatabase &db, student&data);
void insert_date(QSqlDatabase &db, student&data);
void delete_data(QSqlDatabase &db, student&data);
void search_date(QSqlDatabase &db, student&data);
private:
Ui::Widget *ui;
QSqlDatabase db;
};
#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"
#include <QMessageBox>
#include <QSqlRecord>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
db = QSqlDatabase::addDatabase("QMYSQL");
try_connect_database();
student data{"zhao",11,"8班级"};
}
Widget::~Widget()
{
if(db.isOpen()){
db.close();
}
delete ui;
}
bool Widget::try_connect_database(){
db.setHostName("127.0.0.1");
db.setPort(3306);
db.setDatabaseName("students");
db.setUserName("root");
db.setPassword("258551");
bool ok = db.open();
// chinese show
db.setConnectOptions("MYSQL_OPT_RECONNECT=1;MYSQL_SET_CHARSET_NAME=utf8mb4");
if(ok){
return true;
}else{
QMessageBox::information(this, "infor", "link failed");
return false;
}
}
void Widget::update_value(QSqlDatabase &db, student&data){
if(db.isOpen()){
return;
}
QSqlQuery query;
query.prepare("update student SET age = :age WHERE name = :name;");
query.bindValue(":age",data.Age+100,QSql::In);
query.bindValue(":name",data.Name);
query.exec();
qDebug()<<"find execute";
}
void Widget::insert_date(QSqlDatabase &db, student&data){
if(!db.isOpen()){
return;
}
QSqlQuery query;
query.prepare("insert into student (name,age,class) valuse(:Name,:Age,:Class);");
query.bindValue(":Name",data.Name);
query.bindValue(":Age",data.Age);
query.bindValue(":Class",data.Class);
if(query.exec()){
qDebug()<<"insert execute";
}else{
qDebug()<<"failed execute";
}
}
void Widget::delete_data(QSqlDatabase &db, student&data){
if(!db.isOpen()){
return;
}
QSqlQuery query;
query.prepare("DELETE FROM student WHERE name = :name;");
query.bindValue(":name",data.Name);
if(query.exec()){
qDebug()<<"insert execute";
}else{
qDebug()<<"failed execute";
}
};
void Widget::search_date(QSqlDatabase &db, student&data){
if(!db.isOpen()){
return;
}
data.Name = "zhao1";
QSqlQuery query;
query.prepare("DELETE * FROM student WHERE name = :name;");
query.bindValue(":name",data.Name);
query.exec();
QSqlRecord recodes= query.record();
//
while(query.next()){
for(int i = 0 ;i <recodes.count();i++){
QVariant value = query.value(i);
qDebug() << "Column" << i << ":" << value.toString();
}
}
}
void Widget::on_pushButton_clicked()
{
if(!db.isOpen()){
return;
}
QSqlQuery query;
student data{"zhao",11,"8班级"};
query.prepare("update student SET age = :age WHERE name = :name;");
query.bindValue(":age",data.Age+1000,QSql::In);
query.bindValue(":name",data.Name);
if(query.exec()){
qDebug()<<"find execute";
}else{
qDebug()<<"failed execute";
}
}
void Widget::on_pushButton_2_clicked()
{
if(!db.isOpen()){
return;
}
student data{"zhao1",11,"8班级"};
QSqlQuery query;
query.prepare("SELECT * FROM student WHERE name = :name;");
query.bindValue(":name",data.Name);
query.exec();
QSqlRecord recodes= query.record();
//
while(query.next()){
for(int i = 0 ;i <recodes.count();i++){
QVariant value = query.value(i);
qDebug() << "Column" << i << ":" << value.toString();
}
}
}
文章评论