Qt-sqlite数据库实战

一、Qt数据库实战Demo

实验目的: 

掌握QDataBase类的使用方法

掌握QtSQlite的连接方法

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

实验内容: 

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

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

实验步骤

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

连接数据库:

QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");

db.setDatabaseName("test.db");

QSqlQuery query;

if(!db.open())

{

qDebug()<<"Can't open !";

}  

else

qDebug()<<"can open !";          

查询数据库:

QSqlQuery query;

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

查询语句:

insert into test_table values('2','he')

delete from test_table where id ='0' and name ='first'

update test_table set name ='first' where id ='3' 

查看表的使用:

select 条件 from 表名 where 条件   

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

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

 

在表中显示数据的方式

QSqlQueryModel();

modal->setQuery("select * from test_table");

tableview = new QTableView;

tableview->setModel(modal);

实战代码详解:

#ifndef WIDGET_H
#define WIDGET_H

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#include 
#include 
#include 
#include 

#include 

#include 
#include 

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
实现代码:

#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,SIGNAL(clicked()),this,SLOT(deleteData()));
    connect(updateBtn,SIGNAL(clicked()),this,SLOT(updateData()));
    connect(refreshBtn,SIGNAL(clicked()),this,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()<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);
}

运行截图:
Qt-sqlite数据库实战_第1张图片

你可能感兴趣的:(Qt)