Qt:Sqlite的增删改查的基本操作

看看效果图:

Qt:Sqlite的增删改查的基本操作_第1张图片

废话不多说,直接上代码,代码中有很详细的介绍~

#ifndef WIDGET_H
#define WIDGET_H

#include 
#include 
#include 
#include 
#include 

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

private slots:

    void on_findButton_clicked();
    void on_delButton_clicked();
    void on_updateButton_clicked();
    void on_addButton_clicked();

private:
    Ui::Widget *ui;
    QSqlDatabase db;
};

#endif // WIDGET_H

 

#include "widget.h"
#include "ui_widget.h"
#include "itemform.h"
#include 

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    // 1. 根据数据库类型来连接数据库
    db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("student.db");
    // 2. 打开数据库
    if(!db.open())
        qDebug() << "open error" << db.lastError();
    // 3. 根据需求,创建数据库所需要表
    QSqlQuery query;
    //判断student是否已存在,存在则不创建,不存在这创建
    //QString sqlFindTable = QString("select * from sqlite_master where name='%1'").arg("student");  //这个查找的是数据库名
    QString sqlFindTableName = QString("select count(*) from sqlite_master where type = 'table' and name='%1'").arg("student");  //功能同上

    if(!query.exec(sqlFindTableName))
        qDebug() << "findTable error" << db.lastError();
    bool isTableExist = query.exec(sqlFindTableName);
    //qDebug() << isTableExist;
    if(!isTableExist)
    {
        //学生表  字段有id name class
        QString sqlCreate = QString("create table student(id integer primary key autoincrement,"
                                    "name varchar(20),"
                                    "class int);");
        //sql 语句 create table student(id integer primary key autoincrement,name varchar(20),age int,class int);
        if(!query.exec(sqlCreate))
            qDebug() << "create table error" << db.lastError();
    }
#if 0
    //插入数据
    QString sqlInsert = QString("insert into student(name, class)"
                                "values('小生', 1)");
    if(!query.exec(sqlInsert))
        qDebug() << "insert table error" << db.lastError();
#endif

    //删除数据库表
    //QString sqlDrop = QString("drop table student");
    //if(!query.exec(sqlDrop))
        //qDebug() << "create table error" << db.lastError();

}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_findButton_clicked()
{
    ui->listWidget->clear(); //在查询数据之前,清空列表上的内容
    // 1.查询数据库的数据
    QSqlQuery query;
    QString sqlSelect = QString("select * from student;");
    if(!query.exec(sqlSelect))
        qDebug() << "Error search data" << db.lastError();
    // 2.遍历数据库的记录
    while(query.next())
    {
        int id = query.value("id").toInt();
        QString name = query.value("name").toString();
        int classNum = query.value("class").toInt();
        qDebug() << "id:" << id << "name" << name << "classNum" << classNum;
        //3 将数据更新到ui上
        //3.1 创建一个列表头目
       itemForm *studentItem = new itemForm;
       studentItem->setStuInfo(id, name, classNum);
       QListWidgetItem *item = new QListWidgetItem;
       item->setSizeHint(QSize(488, 38));
       ui->listWidget->addItem(item);
       ui->listWidget->setItemWidget(item, studentItem);

    }
}

void Widget::on_delButton_clicked()
{
    QSqlQuery query;
    int id = ui->idLineEdit->text().toInt();
    QString sqlDelete = QString("delete from student where id = %1;").arg(id);
    if(!query.exec(sqlDelete))
        qDebug() << "delete data error" << db.lastError();
    //同时,也要删除UI控件上的内容  下标是从0开始的
    ui->listWidget->takeItem(id - 1);

}

void Widget::on_updateButton_clicked()
{
    ui->listWidget->clear();
    QSqlQuery query;
    QString id = ui->idLineEdit->text();
    QString name = ui->nameLineEdit->text();
    QString sqlUpdate = QString("update student set name = '%1' where id = %2;").arg(name).arg(id.toInt());
    if(!query.exec(sqlUpdate))
        qDebug() << "update data error" << db.lastError();
}

void Widget::on_addButton_clicked()
{
    //[1] 获取ui控件上的内容
    QString name = ui->nameLineEdit->text();
    int classNum = ui->classLineEdit->text().toInt();
    //[2] 将通过数据库接口的访问,将数据插入到数据库中
    QSqlQuery query;
    QString sqlInert = QString("insert into student(name, class)"
                               "values('%1', %2)").arg(name).arg(classNum);
    if(!query.exec(sqlInert))
        qDebug() << "insert data error" << db.lastError();
}

你可能感兴趣的:(程序人生,sqlite,qt,数据库)