Qsqldatabase usage summary, solutions to program crash caused by some database operations

Source: Internet
Author: User

These two days are headaches for the qsqldatabase class. Because multiple connections are connected to the same database, and no names are set for each connection, most simple tutorials on the Internet use the default database connection name. operations on one of the database connections affect the operations on other data connections (they are essentially the same connection) because I use multi-document forms to process different data connections in different forms, removing or changing a connection will also cause other connections to be affected and directly cause the program to crash.

 

 

I have no clue, but I don't know why I wrote the program by referring to the articles of my predecessors. I haven't looked at the corresponding documentation and explained it. As a result, my head is two days old.

 

 

Part of the document information is now posted

 

Qsqldatabase: adddatabase (const qstring & type, const qstring & connectionname = qlatin1string (defaultconnection )) [Static] <br/> adds a database to the list of database connections using the driver type and the connection name connectionname. if there already exists a database connection called connectionname, that connection is removed. </P> <p> the database connection is referred to by connectionname. the newly added database connection is returned. </P> <p> If type is not available or cocould not be loaded, isvalid () returns false. </P> <p> If connectionname is not specified, the New Connection becomes the default connection for the application, and subsequent callto database () without the connection name argument will return the default connection. if a connectionname is provided here, use database (connectionname) to retrieve the connection. </P> <p> warning: if you add a connection with the same name as an existing connection, the new connection replaces the old one. if you call this function more than once without specifying connectionname, the default connection will be the one replaced. </P> <p> before using the connection, it must be initialized. e.g ., call some or all of setdatabasename (), setusername (), setpassword (), sethostname (), setport (), and setconnectoptions (), and, finally, open (). </P> <p> note: this function is thread-safe. </P> <p> See also database (), removedatabase (), and threads and the SQL module.

 

The above is the description of adddatabase. The first is the database connection type. The second is the connection name. If we directly use

Dbconn = qsqldatabase: adddatabase ("qsqlite); <br/> dbconn. setdatabasename ("./dbfinuse/"+ sqlitedb: dbname );

If you set an instance in this way, the connection name is the default data connection name of QT. Of course, this is the root cause of an error in operations on the same database for multiple connected instances.

Therefore, we must connect to the database in the following form:

Dbconn = qsqldatabase: adddatabase ("qsqlite", currentconnname); <br/> dbconn. setdatabasename ("./dbfinuse/" + sqlitedb: dbname );

Currentconnname must be different from other instances. Otherwise, multiple connection operations may fail.

The warning information has been clearly stated.

 Warning: if you add a connection with the same name as an existing connection, the new connection replaces the old one. if you call this function more than once without specifying connectionname, the default connection will be the one replaced.

If the connection name already exists, the previous connection will be replaced.

 

 

 

Let's look at removedatabase.

Void qsqldatabase: removedatabase (const qstring & connectionname) [Static] <br/> Removes the database connection connectionname from the list of database connections. </P> <p> warning: There shocould be no open queries on the database connection when this function is called, otherwise a resource leak will occur. </P> <p> example: </P> <p> // wrong <br/> qsqldatabase DB = qsqldatabase: Database ("sales "); <br/> qsqlquery query ("Select name, DOB from employees", DB); <br/> qsqldatabase: removedatabase ("sales "); // will output a warning </P> <p> // "DB" is now a dangling invalid database connection, <br/> // "query" contains an invalid result set <br/> the correct way to do it: </P> <p >{< br/> qsqldatabase DB = qsqldatabase: Database ("sales"); <br/> qsqlquery query ("Select name, DOB from employees ", DB ); <br/>}< br/> // both "DB" and "query" are destroyed because they are out of scope <br/> qsqldatabase :: removedatabase ("sales"); // correct <br/> to remove the default connection, which may have been created with a call to adddatabase () not specifying a connection name, you can retrieve the default connection name by calling connectionname () on the database returned by database (). note that if a default database hasn' t been created an invalid database will be returned. </P> <p> note: this function is thread-safe. </P> <p> See also database (), connectionname (), and threads and the SQL module. 

 

In this case, removedatabase must also specify the name of the connection to be removed. If multiple connections have been removed, the default data connection name or the same name is used) will cause program errors

You need to release the data resources obtained from the connection before removing it. Otherwise, memory leakage may occur.

 

 

For a qsqldatabase instance with a non-default connection name, when you use query or model for operations, you must set the query or model to use the qsqldatabase. In fact, you must set the connection name.

See the following figure.

Qsqlquery query (dbconn); <br/> query.exe C (SQL _string); <br/> return query;

 

OrModel = new qsqltablemodel (this, DB-> dbconn); <br/> model-> settable (TR ("text table ")); <br/> model-> select (); <br/> UI-> tableview-> setmodel (model ); <br/> UI-> tableview-> hidecolumn (0 );

Model = new qsqlquerymodel (this); <br/> model-> setquery (TR ("select customer number from customer table"), DB-> dbconn );

 

 

 

 

 

 

 

 

 

 

In fact, it's just a bit confusing to post a modified database connection class.

H file

# Ifndef sqlitedb_h <br/> # define sqlitedb_h <br/> # include <qstring> <br/> # include <qtsql> <br/> class sqlitedb <br/> {< br/> public: <br/> sqlitedb (qstring connname); <br/> ~ Sqlitedb (); <br/> static void setdbname (qstring name); <br/> static qstring getdbname (); <br/> qstringlist finddbfiles (); <br/> bool conndb (); <br/> qsqlquery search (qstring SQL _string); <br/> void closedb (); <br/> qsqlquery query; <br/> qsqldatabase dbconn; <br/> qstring currentconnname; <br/> PRIVATE: <br/> static qstring dbname; </P> <p> }; </P> <p> # endif // sqlitedb_h <br/>

 

CPP File

# Include "sqlitedb. H "<br/> # include <qdir> <br/> # include <qtsql> <br/> # include <qstring> <br/> # include <qmessagebox> <br /> qstring sqlitedb:: dbname = ""; <br/> sqlitedb: sqlitedb (qstring connname) <br/>{< br/> currentconnname = connname; <br/>}< br/> sqlitedb ::~ Sqlitedb () <br/>{< br/> If (dbconn. isopen () = true) <br/>{< br/> dbconn. close (); <br/>}< br/> qsqldatabase: removedatabase (dbconn. connectionname (); <br/>}< br/> void sqlitedb: setdbname (qstring name) <br/>{< br/> sqlitedb: dbname = Name; <br/>}< br/> qstring sqlitedb: getdbname () <br/>{< br/> return dbname; <br/>}< br/> qstringlist sqlitedb:: finddbfiles () <br/>{< br/> qdir dir; <br/> dir. setpath (". /DBF Inuse "); <br/> dir. setfilter (qdir: Files | qdir: hidden); <br/> dir. setsorting (qdir: Name); <br/> qstringlist names = dir. entrylist (); <br/> qstringlist newnames; <br/> newnames = names. filter (". DBF "); <br/> return newnames; <br/>}< br/> bool sqlitedb: conndb () <br/>{< br/> If (! Dbconn. isvalid () <br/>{< br/> dbconn = qsqldatabase: adddatabase ("qsqlite", currentconnname); <br/> dbconn. setdatabasename (". /dbfinuse/"+ sqlitedb: dbname); <br/>}< br/> If (dbconn. isopen () = true) <br/>{< br/> return true; <br/>}< br/> else <br/>{< br/> If (! Dbconn. open () <br/>{< br/> return false; <br/>}< br/> else <br/>{< br/> return true; <br/>}< br/> qsqlquery sqlitedb: Search (qstring SQL _string) <br/>{< br/> qsqlquery query (dbconn); <br/> query.exe C (SQL _string); <br/> return query; <br/>}< br/> void sqlitedb: closedb () <br/>{< br/> If (dbconn. isopen () = true) <br/>{< br/> dbconn. close (); <br/>}< br/>

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.