On the internet for a long time about the QT access to SQLite database encapsulation class, but did not find a good access to call class, wrote a out, here to share, I hope to help you, little brother, write code not much experience, if there is anything inappropriate, please criticize that:
Sqliteutil.h
#ifndef Sqliteutil_h#defineSqliteutil_h#include<QSqlDatabase>#include<QSqlQuery>#include<QList>#include<QVariant>#include<QSqlQueryModel>classsqliteutil{ Public: ExplicitSqliteutil (Constqstring& strdatabase ="smart.db"); ~Sqliteutil (); BOOLCreateConnection (Constqstring&strconn); Qsqlrecord Executerecord (Constqstring&strquery); Qsqlrecord Executerecord (Constqstring& strquery, qlist<qvariant>lstparameter); Qsqlrecord Executerecord (Constqstring&strquery, qvariant Parameter); Qsqlquery ExecuteSQLQuery (Constqstring& strquery, qlist<qvariant>lstparameter); Qsqlquery ExecuteSQLQuery (Constqstring&strquery, qvariant Parameter); Qsqlquery ExecuteSQLQuery (Constqstring&strquery); intExecuteint (Constqstring&strquery); intExecuteint (Constqstring& strquery, qlist<qvariant>lstparameter); intExecuteint (Constqstring&strquery, qvariant Parameter); BOOLExecute (Constqstring&strquery, qvariant Parameter); BOOLExecute (Constqstring& strquery, qlist<qvariant>lstparameter); QString executestring (Constqstring&strquery); voidExecutequerymodel (Qsqlquerymodel *p_querymodel,Constqstring&strquery); voidExecutequerymodel (Qsqlquerymodel *p_querymodel,Constqstring&strquery, Qlist<QVariant>lstparameter); voidExecutequerymodel (Qsqlquerymodel *p_querymodel,Constqstring&strquery, Qvariant Parameter);Private: Qsqldatabase m_db; QString m_strdatabase; };#endif //Sqliteutil_h
Sqliteutil.cpp
#include"sqliteutil.h"#include<QDebug>/******************************************************************************** Copyright (C) 2014 scutemos-huabo** All rights reserved.** Contact: [email protected]**. Keep the author contact information.** 2014-07 -16******************************************************************************/Sqliteutil::sqliteutil (ConstQString &strdatabase): M_strdatabase (strdatabase) {createconnection (m_strdatabase);} Sqliteutil::~Sqliteutil () {m_db.close ();}BOOLSqliteutil::createconnection (ConstQString &database) { //establish a connection to the database if(Qsqldatabase::contains ("My_conn") ) {m_db= Qsqldatabase::d atabase ("My_conn"); } Else{m_db= Qsqldatabase::adddatabase ("Qsqlite","My_conn"); } //Set database namem_db.setdatabasename (database); //Open Database if(!M_db.open ()) {Qdebug ()<<"Open Database failed!"; return false; } Else { return true; }}qsqlrecord Sqliteutil::executerecord (Constqstring&strquery) { if(!M_db.isopen ()) {createconnection (m_strdatabase); } qsqlquery query (m_db); Query.prepare (strquery); Query.exec (); returnQuery.record ();} Qsqlrecord Sqliteutil::executerecord (Constqstring& strquery, qlist<qvariant>lstparameter) { if(!M_db.isopen ()) {createconnection (m_strdatabase); } qsqlquery query (m_db); Query.prepare (strquery); for(inti =0; I < Lstparameter.count (); i++) Query.bindvalue (i, lstparameter[i]); Query.exec (); returnQuery.record ();} Qsqlrecord Sqliteutil::executerecord (Constqstring&strquery, Qvariant Parameter) { if(!M_db.isopen ()) {createconnection (m_strdatabase); } qsqlquery query (m_db); Query.prepare (strquery); Query.bindvalue (0, Parameter); Query.exec (); returnQuery.record ();} Qsqlquery Sqliteutil::executesqlquery (Constqstring& strquery, qlist<qvariant>lstparameter) { if(!M_db.isopen ()) {createconnection (m_strdatabase); } qsqlquery query (m_db); Query.prepare (strquery); Query.setforwardonly (true); for(inti =0; I < Lstparameter.count (); i++) Query.bindvalue (i, lstparameter[i]); Query.exec (); returnquery;} Qsqlquery Sqliteutil::executesqlquery (Constqstring&strquery, Qvariant Parameter) { if(!M_db.isopen ()) {createconnection (m_strdatabase); } qsqlquery query (m_db); Query.setforwardonly (true); Query.prepare (strquery); Query.bindvalue (0, Parameter); Query.exec (); returnquery;} Qsqlquery Sqliteutil::executesqlquery (Constqstring&strquery) { if(!M_db.isopen ()) {createconnection (m_strdatabase); } qsqlquery query (m_db); Query.setforwardonly (true); Query.prepare (strquery); Query.exec (); returnquery;}intSqliteutil::executeint (Constqstring&strquery) { if(!M_db.isopen ()) {createconnection (m_strdatabase); } qsqlquery query (m_db); Query.prepare (strquery); Query.exec (); intID =0; while(Query.next ()) {ID= Query.value (0). ToInt (); } returnID;}intSqliteutil::executeint (Constqstring& strquery, qlist<qvariant>lstparameter) { if(!M_db.isopen ()) {createconnection (m_strdatabase); } qsqlquery query (m_db); Query.prepare (strquery); for(inti =0; I < Lstparameter.count (); i++) Query.bindvalue (i, lstparameter[i]); Query.exec (); intID =0; while(Query.next ()) {ID= Query.value (0). ToInt (); } returnID;}intSqliteutil::executeint (Constqstring&strquery, Qvariant Parameter) { if(!M_db.isopen ()) {createconnection (m_strdatabase); } qsqlquery query (m_db); Query.prepare (strquery); Query.bindvalue (0, Parameter); Query.exec (); intID =0; while(Query.next ()) {ID= Query.value (0). ToInt (); } returnID;}BOOLSqliteutil::execute (Constqstring&strquery, Qvariant Parameter) { if(!M_db.isopen ()) {createconnection (m_strdatabase); } qsqlquery query (m_db); Query.prepare (strquery); Query.bindvalue (0, Parameter); returnquery.exec ();}BOOLSqliteutil::execute (Constqstring& strquery, qlist<qvariant>lstparameter) { if(!M_db.isopen ()) {createconnection (m_strdatabase); } qsqlquery query (m_db); Query.prepare (strquery); for(inti =0; I < Lstparameter.count (); i++) Query.bindvalue (i, lstparameter[i]); returnquery.exec ();} QString sqliteutil::executestring (Constqstring&strquery) { if(!M_db.isopen ()) {createconnection (m_strdatabase); } qsqlquery query (m_db); Query.prepare (strquery); Query.exec (); QString temp; while(Query.next ()) {temp= Query.value (0). toString (); } returntemp;}voidSqliteutil::executequerymodel (Qsqlquerymodel *p_querymodel,Constqstring&strquery) { if(!M_db.isopen ()) {createconnection (m_strdatabase); } P_querymodel-setquery (strquery, m_db);}voidSqliteutil::executequerymodel (Qsqlquerymodel *p_querymodel,Constqstring&strquery, Qlist<QVariant>lstparameter) { if(!M_db.isopen ()) {createconnection (m_strdatabase); } qsqlquery query (m_db); Query.prepare (strquery); for(inti =0; I < Lstparameter.count (); i++) Query.bindvalue (i, lstparameter[i]); P_querymodel-setquery (query);}voidSqliteutil::executequerymodel (Qsqlquerymodel *p_querymodel,Constqstring&strquery, Qvariant Parameter) { if(!M_db.isopen ()) {createconnection (m_strdatabase); } qsqlquery query (m_db); Query.prepare (strquery); Query.bindvalue (0, Parameter); P_querymodel-setquery (query);}