[QT] QT SQLite 实现增删改查

本文详细介绍了使用Qt库结合SQL在个人站点中创建数据库连接、初始化页面设计,以及实现了包括查询、插入、更新和删除操作的窗口功能。通过实例展示了如何在Qt应用程序中操作SQLite数据库,并处理了错误和数据交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

个人站点原文

1.在工程文件下使用sql

*.pro 输入QT+=sql

2.初始化页面设计

3. 写代码

3.1 连接数据库的头文件书写

#ifndef CONNECTION_H
#define CONNECTION_H

#include <QSqlDatabase>
#include <QMessageBox>
#include <QSqlQuery>

static bool CreateConnectDatabase(){
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("test.db");
    if( !db.open() ) {
        QMessageBox box;
        box.setText("open database fail");
        return false;
    }
    QSqlQuery query;
    query.exec("create table SQLite(id integer primary key, name varchar(20) )");
    return true;
}

#endif

3.2 主函数调用

#include <QtGui/QApplication>
#include "mainwindow.h"
#include "connection.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    if(! CreateConnectDatabase() ){
        return false;
    }
    MainWindow w;
    w.show();
    return a.exec();
}

3.3 窗口头文件定义槽函数

private slots:
    void on_lineEdit_1_textChanged(QString value);
    void on_lineEdit_2_textChanged(QString value);
    void on_Button_1_clicked();
    void on_Button_2_clicked();
    void on_Button_3_clicked();
    void on_Button_4_clicked();
    void on_Button_5_clicked();

3.4 实现槽函数

void MainWindow::on_lineEdit_1_textChanged(QString value){
    idValue = value;
}
void MainWindow::on_lineEdit_2_textChanged(QString value){
    nameValue = value;
}
// show
void MainWindow::on_Button_1_clicked(){
    QSqlQuery query;
    query.exec("select id,name from SQLite");
    QStandardItemModel *model = new QStandardItemModel;
    this->ui->tableView->setModel(model);
    model->setHorizontalHeaderItem(0, new QStandardItem("id"));
    model->setHorizontalHeaderItem(1, new QStandardItem("name"));
    int i = 0;
    while( query.next() ){

        ui->label->setText("select ok");
        model->setItem(i, 0, new QStandardItem( query.value(0).toString() ) );
        model->setItem(i, 1, new QStandardItem( query.value(1).toString() ) );
        i += 1;
    }
}


void MainWindow::on_Button_2_clicked(){
    if(idValue == "" || nameValue == ""){
        ui->label->setText("id or name is none");
    }else{
        QSqlQuery query;
        query.exec("select id from SQLite where id='" + idValue + "'");
        if(! query.next()){
            query.exec("insert into SQLite(id, name) values( " + idValue + ",'" + nameValue + "')");
            ui->label->setText("insert id=" + QString(idValue) +" success");
            ui->lineEdit_1->setText("");
            ui->lineEdit_2->setText("");
        }else{
            ui->label->setText("insert existed");
        }
    }
}
void MainWindow::on_Button_3_clicked(){
    if(idValue == "" || nameValue == ""){
        ui->label->setText("id or name is none");
    }else{
        QSqlQuery query;
        query.exec("select id from SQLite where id='" + idValue + "'");
        if( query.next()){
            query.exec("update SQLite set name = '" + nameValue + "' where id=" + idValue );
            ui->label->setText("update id=" + QString(idValue) +" success");
            ui->lineEdit_1->setText("");
            ui->lineEdit_2->setText("");
        }else{
            ui->label->setText("update not existed");
        }
    }
}
void MainWindow::on_Button_4_clicked(){
    QSqlQuery query;
    query.exec("delete from SQLite");
    ui->label->setText("delete ok");

}

void MainWindow::on_Button_5_clicked(){
     qApp->quit();
}

4 运行

此时没有任何数据
外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-EYiVAsbW-1656584749879)

插入数据 1 123

https://fastly.jsdelivr.net/gh/xing403/iotblog@master/assets/website/20220615180442.png

查询数据

https://fastly.jsdelivr.net/gh/xing403/iotblog@master/assets/website/20220615180535.png

更新数据 1 456

https://fastly.jsdelivr.net/gh/xing403/iotblog@master/assets/website/20220615181024.png

删除数据(删除所有数据)

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

星如雨l

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值