[QT] QT SQLite 实现增删改查

个人站点原文

1.在工程文件下使用sql

*.pro 输入QT+=sql

2.初始化页面设计

[QT] QT SQLite 实现增删改查_第1张图片

3. 写代码

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

#ifndef CONNECTION_H
#define CONNECTION_H

#include 
#include 
#include 

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 
#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 运行

此时没有任何数据
[QT] QT SQLite 实现增删改查_第2张图片

插入数据 1 123

[QT] QT SQLite 实现增删改查_第3张图片

查询数据

[QT] QT SQLite 实现增删改查_第4张图片

更新数据 1 456

[QT] QT SQLite 实现增删改查_第5张图片

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

[QT] QT SQLite 实现增删改查_第6张图片

你可能感兴趣的:(笔记,sqlite,数据库,qt)