Qt-sqlite数据库实战

前端之家收集整理的这篇文章主要介绍了Qt-sqlite数据库实战前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

一、Qt数据库实战Demo

实验目的:

掌握QDataBase类的使用方法

掌握Qtsqlite的连接方法

掌握QTablewidget等项视图类的使用方法

实验内容

1.QT的连接与查询数据库方法,查看表的使用

2.使用窗体编辑记录的方法以及在表中显示数据的方式

实验步骤

1.QT的连接与查询数据库方法,查看表的使用

连接数据库

QsqlDatabasedb=QsqlDatabase::addDatabase("QsqlITE");

db.setDatabaseName("test.db");

QsqlQueryquery;

if(!db.open())

{

qDebug()<<"Can'topen!";

}

else

qDebug()<<"canopen!";

查询数据库

QsqlQueryquery;

query.exec(“/*sql语句*/”)

查询语句:

insertintotest_tablevalues('2','he')

deletefromtest_tablewhereid='0'andname='first'

updatetest_tablesetname='first'whereid='3'

查看表的使用:

select条件from表名where条件

2.使用窗体编辑记录的方法以及在表中显示数据的方式

窗体编辑记录主要使用到信号与槽、QLineEdit,主要是通过通过lineedit->text().tostring进行转化然后结合sql语句进行操作,通过click事件触发相应的槽函数的触发实现。

在表中显示数据的方式

QsqlQueryModel();

modal->setQuery("select*fromtest_table");

tableview=newQTableView;

tableview->setModel(modal);

实战代码详解:

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <qsql.h>
#include <QsqlError>
#include <QsqlDatabase>
#include <QsqlQuery>
#include <QsqlDriver>
#include <QDebug>
#include <QFile>
#include <QsqlQueryModel>

#include <QPushButton>
#include <QLineEdit>
#include <QLabel>
#include <QMessageBox>

#include <QTableView>

#include <QHBoxLayout>
#include <QVBoxLayout>

class Widget : public QWidget
{
    Q_OBJECT
    
public:
    Widget(QWidget *parent = 0);
    ~Widget();
public:
    void ConnectDB();
    bool CreateTB();
    bool InsertData(QString id,QString name);
    bool SelectData();
    bool deleData(QString id,QString name);
    bool updData(QString id,QString name);
private slots:
    void addData();
    void deleteData();
    void updateData();
    void refreshData();
private:
    QPushButton *addBtn;
    QPushButton *delteBtn;
    QPushButton *updateBtn;
    QPushButton *refreshBtn;

    QLineEdit *idLine;
    QLineEdit *nameLine;

    QLabel *id;
    QLabel *name;

    QsqlQueryModel *modal;
    QTableView *tableview;
};

#endif // WIDGET_H
实现代码
</pre><pre name="code" class="cpp">
#include "widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{

    ConnectDB();
    CreateTB();

    addBtn = new QPushButton(tr("add"));
    delteBtn = new QPushButton(tr("delete"));
    updateBtn = new QPushButton(tr("update"));
    refreshBtn = new QPushButton(tr("refresh"));

    idLine = new QLineEdit;
    nameLine = new QLineEdit;

    id = new QLabel(tr("id:"));
    name = new QLabel(tr("name:"));


    modal = new QsqlQueryModel();
    modal->setQuery("select * from test_table order by id");
    tableview = new QTableView;
    tableview->setModel(modal);

    QHBoxLayout *hBox1 = new QHBoxLayout;
    hBox1->addWidget(addBtn);
    hBox1->addWidget(delteBtn);
    hBox1->addWidget(updateBtn);
    hBox1->addWidget(refreshBtn);

    QHBoxLayout *hBox2 = new QHBoxLayout;
    hBox2->addWidget(id);
    hBox2->addWidget(idLine);
    hBox2->addWidget(name);
    hBox2->addWidget(nameLine);


    QVBoxLayout *vBox = new QVBoxLayout;
    vBox->addWidget(tableview);
    vBox->addLayout(hBox1);
    vBox->addLayout(hBox2);

    setLayout(vBox);

    connect(addBtn,SIGNAL(clicked()),this,SLOT(addData()));
    connect(delteBtn,SLOT(deleteData()));
    connect(updateBtn,SLOT(updateData()));
    connect(refreshBtn,SLOT(refreshData()));
}

void Widget::ConnectDB()
{
    QsqlDatabase db = QsqlDatabase::addDatabase("QsqlITE");
    db.setDatabaseName("test.db");

    QsqlQuery query;

    if(db.open())
    {
        qDebug()<<"open !";
    }
    else
    {
        qDebug()<<"can't open !";
    }

}

bool Widget::CreateTB()
{
    QsqlQuery query;
    bool success = query.exec("create table if not exists test_table (id int primary key,name varchar)");

    if(!success)
    {
        qDebug()<<"create table false";
        qDebug() << query.lastError().text();
        return false;
    }
    else
    {
        qDebug()<<"create table success";
        QMessageBox::warning(this,"Error","create successful");
        return true;
    }
}

bool Widget::InsertData(QString id,QString name)
{
    QsqlQuery query;

    QString str;

    str = "insert into test_table values('";
    str += id;
    str += "','";
    str += name;
    str += "')";
    //测试专用
    qDebug() << str;
    bool s = query.exec(str);
    qDebug()<<s;

    if(s)
    {
        QMessageBox::warning(this,"conduct successful");
        return false;
    }

    QMessageBox::information(this,"message","conduct successful!");
    return true;

}

bool Widget::SelectData()
{
    QsqlQuery query;
    bool success = query.exec("select * from test_table");
    if(!success)
    {
        return false;
    }
    while(query.next())//query.next()指向查找到的第一条记录,然后每次后移一条记录
    {
        QString ele0=query.value(0).toString();//query.value(0)是id的值,将其转换为int型
        QString ele1=query.value(1).toString();
        qDebug()<<ele0<<ele1;//输出两个值
    }
    return true;
}


bool Widget::deleData(QString id,QString name)
{
    QsqlQuery query;

    QString str;

    str = "delete from test_table where id ='";
    str += id;
    str +="' and name ='";
    str += name;
    str +="'";
//测试专用
    qDebug() << str;
    bool s = query.exec(str);
    qDebug()<<s;

    if(!s)
    {

        return false;
    }

    return true;
}

bool Widget::updData(QString id,QString name)
{
    QsqlQuery query;

    QString str;

    str = "update test_table set name ='";
    str += name;
    str +="' where id ='";
    str += id;
    str +="'";
//测试专用
    qDebug() << str;
    bool s = query.exec(str);
    qDebug()<<s;

    if(!s)
        return false;

    return true;

}

void Widget::addData()
{
    QString Gid,Gname;
    Gid = idLine->text();
    Gname = nameLine->text();

    InsertData(Gid,Gname);
}

void Widget::deleteData()
{

    QString Did,Dname;
    Did = idLine->text();
    Dname = nameLine->text();

    deleData(Did,Dname);

}

void Widget::updateData()
{
    QString Uid,Uname;
    Uid = idLine->text();
    Uname = nameLine->text();

    updData(Uid,Uname);
}

void Widget::refreshData()
{
    modal->setQuery("select * from test_table order by id");
    //tableview = new QTableView;
    tableview->setModel(modal);
}

运行截图:

原文链接:https://www.f2er.com/sqlite/200448.html

猜你在找的Sqlite相关文章