掌握QDataBase类的使用方法
掌握Qt与sqlite的连接方法
掌握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" >
#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);}运行截图:
总结
以上是内存溢出为你收集整理的Qt-sqlite数据库实战全部内容,希望文章能够帮你解决Qt-sqlite数据库实战所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)