Qt implements cascading deletion of sqlite3 and qt implements sqlite3
1. there are two required tables: the barcode table and the product details table. The rfid in the barcode table is the rfid foreign key in the product table, you must delete the related entries in the product table to achieve cascading deletion of the bar code table. solve this problem by using the cascading deletion feature provided by sqlite3 in qt. 3. code 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.exe c ();} bool createdb () {bool bret = false; // create a database directory to store the database file QString strdbpath (""); strdbpath = QCoreApplication: applicat IonDirPath (); 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.exe c ("PRAGMA foreign_keys = ON;") {QSqlError sqlerror = setquery. lastError (); QString texterr = sqlerror. text (); QMessageBox: information (nullptr, "errormsg", texterr); return false ;} // item Details table bret = setquery.exe c ("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 ;}} // The bar code table and the skuid table are not used as the primary key because it may involve many to many pairs. // the bar code table bret = setquery.exe c ("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_H
Mysql. 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, Qt: Horizontal, QStringLiteral ("name"); m_barcodemodel.setQuery ("s ") Elect * from tb_barcode ", QSqlDatabase: database (" file ", true); m_barcodemodel.setHeaderData (0, Qt: Horizontal, QStringLiteral (" id "); m_barcodemodel.setHeaderData (1, qt: Horizontal, QStringLiteral ("Scan Code"); m_barcodemodel.setHeaderData (2, Qt: Horizontal, 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.exe c ("insert into tb_goods values ('20160301', 'aaa')"); setquery.exe c ("insert into tb_goods values ('20160301', 'aaeffdda ')"); setquery.exe c ("insert into tb_barcode (barcode, rfid) values ('adddaeid', '20170101')"); reflushModel ();} // The problem here should not be very serious void mysql: on_deleteButton_clicked () {QSqlQ Uery setquery (QSqlDatabase: database ("file", true); setquery.exe c ("delete from tb_goods where rfid = '000000'"); reflushModel ();} void mysql:: on_updateButton_clicked () {QSqlQuery setquery (QSqlDatabase: database ("file", true )); setquery.exe c ("update tb_goods set name = 'dddddd' where rfid = '000000'"); reflushModel ();} void mysql: reflushModel () {m_goodsmodel.setQuery ("select * from tb_goods", QSqlD Atabase: database ("file", true); m_barcodemodel.setQuery ("select * from tb_barcode", QSqlDatabase: database ("file", true);} void mysql :: setAttibutes (QTableView * pView, int nHeaderHeight, int nColumnHeight) {if (pView = NULL) {QString strTemp = QStringLiteral ("pView = NULL! An error occurred while setting table properties! "); Return;} pView-> horizontalHeader ()-> setFixedHeight (nHeaderHeight); // set the height of the header pView-> horizontalHeader ()-> setSectionsClickable (false ); // set the header to be unclickable (sorted by default) pView-> horizontalHeader ()-> setStretchLastSection (true ); // The last column is filled with the remaining blank pView-> setSelectionBehavior (q1_actitemview: SelectRows); // You can select a row of pView-> setEditTriggers (q1_actitemview: NoEditTriggers) at each time ); // make the table view read-only pView-> verticalHeader ()-> setdefasesectionsize (nColumnHeight); // set the Row Height pView-> setAlternatingRowColors (true ); // you can display pView in alternate colors-> setShowGrid (false); // you can set pView to not display the grid line-> setCornerButtonEnabled (false); // the button in the upper left corner is unavailable, click this button to select pView> horizontalScrollBar ()> setStyleSheet ("QScrollBar: horizontal {height: 10px; background: transparent; background-color: rgb (248,248,248 ); margin: 0px, 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,248,248); margin: 0px, 0px, 0px, 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. remarks 1. complete code download unzip 5.40 + win7 compiled by 4. Refer to the http://bbs.csdn.net/topics/391065975
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.