1.需求
有两张表,条码表和产品详情表,条码表中的rfid为产品表中的rfid外键,要求删除产品表中的相关条目时能实现条码表的级联删除
mymain.cpp
#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;
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()) {
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
{
return bret;
}
}
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;
}
#ifndef MysqL_H
#define MysqL_H
#include <QtWidgets/QMainWindow>
#include <QsqlQueryModel>
#include "ui_MysqL.h"
class MysqL : public QMainWindow
{
Q_OBJECT
public:
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_H
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.备注
2.还存在的问题使用自带的自增作为主键可能会出现越界的问题,不过integer最大为9223372036854775807如果数据量不是很大的话可以不必考虑
3.在vs2010+qt 5.40 + win7下编译通过