#include "MysqL.h"#include <QtWidgets/QApplication>#include <QsqlDatabase>#include <QsqlError>#include <Qsqlquery>#include <QtCore/QDir>#include <QMessageBox>bool createdb();int main(int argc,char *argv[]){ QApplication a(argc,argv); createdb(); MysqL w; w.show(); return a.exec();}bool createdb(){ bool bret = false; //这里创建一个db目录存储数据库文件 QString strdbpath(""); strdbpath = QCoreApplication::applicationDirPath(); strdbpath += "/db"; QDir dir(""); dir.mkpath(strdbpath); strdbpath += "/MysqLite.db"; QsqlDatabase dbset = QsqlDatabase::addDatabase("QsqlITE","file"); dbset.setDatabasename(strdbpath); if (!dbset.open()) { //LOG_ALL_ERROR(QStringliteral("Failed open MysqLite.db")); return bret; } Qsqlquery setquery(QsqlDatabase::database("file",true)); if(!setquery.exec("PRAGMA foreign_keys = ON;")) { QsqlError sqlerror = setquery.lastError(); QString texterr = sqlerror.text(); QMessageBox::information(nullptr,"errormsg",texterr); return false; } //商品详情表 bret = setquery.exec("create table tb_goods(rfID varchar(33) primary key,name varchar(200) not null)"); if (!bret) { QsqlError sqlerror = setquery.lastError(); QString texterr = sqlerror.text(); if (texterr.contains("already exists",Qt::CaseInsensitive)) bret = true; else { //LOG_ALL_ERROR(QStringliteral("Failed create table tb_goods.")); return bret; } } //这里条码对照表和skuID对照表因为可能牵涉到多对多的情况 故没有作为主键 //条码对照表 bret = setquery.exec("create table tb_barcode(ID INTEGER PRIMARY KEY autoINCREMENT," "barcode varchar(100) not null,rfID varchar(33) not null," "FOREIGN KEY(rfID) REFERENCES tb_goods(rfID) ON DELETE cascade)"); if (!bret) { QsqlError sqlerror = setquery.lastError(); QString texterr = sqlerror.text(); if (texterr.contains("already exists",Qt::CaseInsensitive)) bret = true; else { return bret; } } return bret;}MysqL.h
#ifndef MysqL_H#define MysqL_H#include <QtWidgets/QMainWindow>#include <QsqlqueryModel>#include "ui_MysqL.h"class MysqL : public QMainWindow{ Q_OBJECTpublic: MysqL(QWidget *parent = 0); ~MysqL();private slots: voID on_insertbutton_clicked(); voID on_deletebutton_clicked(); voID on_updatebutton_clicked();private: voID reflushModel(); voID setAttibutes(QtableVIEw* pVIEw,int nheaderHeight = 45,int nColumnHeight = 45);private: Ui::MysqLClass ui; QsqlqueryModel m_goodsmodel; QsqlqueryModel m_barcodemodel;};#endif // MysqL_HMysqL.cpp
#include "MysqL.h"#include <QsqlError>#include <Qsqlquery>#include <QScrollbar>MysqL::MysqL(QWidget *parent) : QMainWindow(parent){ ui.setupUi(this); m_goodsmodel.setquery("select * from tb_goods",QsqlDatabase::database("file",true)); m_goodsmodel.setheaderData(0,Qt::Horizontal,QStringliteral("ID")); m_goodsmodel.setheaderData(1,QStringliteral("名称")); m_barcodemodel.setquery("select * from tb_barcode",true)); m_barcodemodel.setheaderData(0,QStringliteral("ID")); m_barcodemodel.setheaderData(1,QStringliteral("扫描码")); m_barcodemodel.setheaderData(2,QStringliteral("rfID")); ui.goodVIEw->setModel(&m_goodsmodel); ui.skuIDVIEw->setModel(&m_barcodemodel); setAttibutes(ui.goodVIEw); setAttibutes(ui.skuIDVIEw);}MysqL::~MysqL(){}voID MysqL::on_insertbutton_clicked(){ Qsqlquery setquery(QsqlDatabase::database("file",true)); setquery.exec("insert into tb_goods values('123456789','aaa')"); setquery.exec("insert into tb_goods values('23456789','aaeffdda')"); setquery.exec("insert into tb_barcode(barcode,rfID) values('adddaeee','123456789')"); reflushModel();}//这里应该问题不是很大voID MysqL::on_deletebutton_clicked(){ Qsqlquery setquery(QsqlDatabase::database("file",true)); setquery.exec("delete from tb_goods where rfID='123456789'"); reflushModel();}voID MysqL::on_updatebutton_clicked(){ Qsqlquery setquery(QsqlDatabase::database("file",true)); setquery.exec("update tb_goods set name='redddd' where rfID='123456789'"); reflushModel();}voID MysqL::reflushModel(){ m_goodsmodel.setquery("select * from tb_goods",true)); m_barcodemodel.setquery("select * from tb_barcode",true));}voID MysqL::setAttibutes(QtableVIEw* pVIEw,int nheaderHeight,int nColumnHeight){ if ( pVIEw == NulL ) { QString strTemp = QStringliteral("pVIEw== NulL!设置表格属性失败!"); return; } pVIEw->horizontalheader()->setFixedHeight(nheaderHeight); //设置表头的高度 pVIEw->horizontalheader()->setSectionsClickable(false); //设置表头不可点击(默认点击后进行排序) pVIEw->horizontalheader()->setStretchLastSection(true); //最后一列占满剩余空白 pVIEw->setSelectionBehavior(QAbstractItemVIEw::SelectRows); //设置选择行为时每次选择一行 pVIEw->setEditTriggers(QAbstractItemVIEw::NoEditTriggers); //使表视图只读 pVIEw->verticalheader()->setDefaultSectionSize(nColumnHeight); //设置行高 pVIEw->setAlternatingRowcolors(true); //可以交替颜色显示 pVIEw->setShowGrID(false); //设置不显示格子线 pVIEw->setCornerbuttonEnabled(false); //左上角的按钮不可用,此按钮功能,一点击,全选 pVIEw->horizontalScrollbar()->setStyleSheet( "QScrollbar:horizontal{height:10px;background:transparent;background-color:rgb(248,248,248);margin:0px,0px,0px;padding-left:10px;padding-right:10px;}" "QScrollbar::handle:horizontal{height:10px;background:lightgray;border-radius:5px;/*min-height:20;*/}" "QScrollbar::handle:horizontal:hover{height:10px;background:gray;border-radius:5px;/*min-height:20;*/}" "QScrollbar::add-line:horizontal{/*height:10px;wIDth:10px;*/border-image:url(:/button/images/button/right.png);/*subcontrol-position:right;*/}" "QScrollbar::sub-line:horizontal{/*height:10px;wIDth:10px;*/border-image:url(:/button/images/button/left.png);/*subcontrol-position:left;*/}" "QScrollbar::add-line:horizontal:hover{/*height:10px;wIDth:10px;*/border-image:url(:/button/images/button/right_mouseDown.png);/*subcontrol-position:right;*/}" "QScrollbar::sub-line:horizontal:hover{/*height:10px;wIDth:10px;*/border-image:url(:/button/images/button/left_mouseDown.png);/*subcontrol-position:left;*/}" "QScrollbar::add-page:horizontal,QScrollbar::sub-page:horizontal{background:transparent;border-radius:5px;}" ); pVIEw->verticalScrollbar()->setStyleSheet( "QScrollbar:vertical{wIDth:10px;background:transparent;background-color:rgb(248,0px;padding-top:10px;padding-bottom:10px;}" "QScrollbar::handle:vertical{wIDth:10px;background:lightgray ;border-radius:5px;min-height:20;}" "QScrollbar::handle:vertical:hover{wIDth:10px;background:gray;border-radius:5px;min-height:20;}" "QScrollbar::add-line:vertical{height:10px;wIDth:10px;border-image:url(:/button/images/button/down.png);subcontrol-position:bottom;}" "QScrollbar::sub-line:vertical{height:10px;wIDth:10px;border-image:url(:/button/images/button/up.png);subcontrol-position:top;}" "QScrollbar::add-line:vertical:hover{height:10px;wIDth:10px;border-image:url(:/button/images/button/down_mouseDown.png);subcontrol-position:bottom;}" "QScrollbar::sub-line:vertical:hover{height:10px;wIDth:10px;border-image:url(:/button/images/button/up_mouseDown.png);subcontrol-position:top;}" "QScrollbar::add-page:vertical,QScrollbar::sub-page:vertical{background:transparent;border-radius:5px;}" );}4.备注 1.完整的代码下载http://download.csdn.net/detail/zhang_ruiqiang/8939977 2.还存在的问题使用自带的自增作为主键可能会出现越界的问题,不过integer最大为9223372036854775807如果数据量不是很大的话可以不必考虑 3.在vs2010+qt 5.40 + win7下编译通过 4.参考http://bbs.csdn.net/topics/391065975 总结
以上是内存溢出为你收集整理的qt实现sqlite3的级联删除全部内容,希望文章能够帮你解决qt实现sqlite3的级联删除所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)