QT implementation of SQLITE3 cascade Delete

Source: Internet
Author: User
Tags comparison table reserved

1. The demand has two tables, bar code table and Product detail table, the barcode table RFID for the product table RFID foreign key, the request to delete the product table related items can realize the bar code table Cascade delete 2. Resolves this requirement by using a cascade delete from QT sqlite3 3. Code Mymain.cpp
#include "mysql.h"#include <QtWidgets/QApplication>#include <QSqlDatabase>#include <QSqlError>#include <QSqlQuery>#include <QtCore/QDir>#include <QMessageBox>BOOLCreatedb ();intMainintargcChar*argv[]) {qapplication A (argc, argv);    Createdb ();    MySQL W; W.show ();returnA.exec ();}BOOLCreatedb () {BOOLBret =false;//Create a DB directory store database file hereQString 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"));        returnBret } qsqlquery setquery (qsqldatabase::d atabase ("File",true));if(!setquery.exec ("PRAGMA foreign_keys = on;") {Qsqlerror SQLError = Setquery.lasterror ();         QString Texterr = Sqlerror.text (); Qmessagebox::information (nullptr,"ErrorMsg", Texterr);return false; }//commodity Details tableBret = Setquery.exec ("CREATE TABLE Tb_goods (RFID varchar (primary) key, name varchar () 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."));            returnBret }    }//Here bar code Comparison table and Skuid table because there may be many-to-many cases are not as primary key    //Barcode TableBret = Setquery.exec ("CREATE TABLE Tb_barcode (ID INTEGER PRIMARY KEY autoincrement,"                         "Barcode varchar (+) NOT NULL, RFID varchar (+) 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{returnBret }    }returnBret;}
Mysql.h
#ifndef Mysql_h#define mysql_h#include <QtWidgets/QMainWindow>#include <QSqlQueryModel>#include "ui_mysql.h"Class MySQL: Publicqmainwindow{Q_object Public:MySQL(Qwidget *parent =0); ~mysql ();PrivateSlotsvoid on_insertbutton_clicked();voidOn_deletebutton_clicked ();voidOn_updatebutton_clicked ();Private:void Reflushmodel();voidSetattibutes (qtableview* PView,intNheaderheight = $,intNcolumnheight = $);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::d atabase ("File",true)); M_goodsmodel.setheaderdata (0,Qt:: Horizontal, Qstringliteral ("id")); M_goodsmodel.setheaderdata (1,Qt:: Horizontal, Qstringliteral ("Name")); M_barcodemodel.setquery ("SELECT * from Tb_barcode",Qsqldatabase::d atabase ("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::d atabase ("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 ();}//It shouldn't be a big problem here.void MySQL:: On_deletebutton_clicked () {Qsqlquery setquery (Qsqldatabase::d atabase ("File",true)); Setquery.exec ("Delete from tb_goods where rfid= ' 123456789 '"); Reflushmodel ();}void MySQL:: On_updatebutton_clicked () {Qsqlquery setquery (Qsqldatabase::d atabase ("File",true)); Setquery.exec ("Update tb_goods set name= ' redddd ' where rfid= ' 123456789 '"); Reflushmodel ();}void MySQL:: Reflushmodel () {M_goodsmodel.setquery ("SELECT * from Tb_goods",Qsqldatabase::d atabase ("File",true)); M_barcodemodel.setquery ("SELECT * from Tb_barcode",Qsqldatabase::d atabase ("File",true));}void MySQL:: Setattibutes (qtableview* pView, int nheaderheight, int ncolumnheight) {if(PView = = NULL) {QString strtemp = qstringliteral ("pview== null! Failed to set table properties! ");return; } Pview->horizontalheader ()Setfixedheight (Nheaderheight);//Sets the height of the header Pview->horizontalheader ()Setsectionsclickable (false);//Set header is not clickable (sort by default click) Pview->horizontalheader ()Setstretchlastsection (true);//The last column fills the remaining blank Pview->setselectionbehavior (Qabstractitemview:: Selectrows);//When you set the selection behavior, select one line at a time pview->setedittriggers (Qabstractitemview:: Noedittriggers);//Make Table View read-only Pview->verticalheader ()Setdefaultsectionsize (Ncolumnheight);//Set line height pview->setalternatingrowcolors (true);//You can alternate colors to display Pview->setshowgrid (false);//Setting does not display the grid pview->setcornerbuttonenabled (false);//The button in the upper left corner is not available, this button function, a click, select All 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. Note 1. Complete code download http://download.csdn.net/detail/zhang_ruiqiang/89399772. Problems that still exist using the self-increment as the primary key may be out of bounds, But the integer maximum is 9223372036854775807 if the amount of data is not very large can not be considered 3.5.40 + win7 under VS2010+QT 4. Reference http://bbs.csdn.net/topics/391065975

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

QT implementation of SQLITE3 cascade Delete

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.