The QT database generated by qsqldatabase::adddatabase () Qsqldatabase can only be used in the thread that created it, and it is not supported to use a connection in multithreading or to create a query in another thread
Suppose you have the following code: BOOL OpenDatabase () { qsqldatabase db; QString connectionname = "SQLite"; db = Qsqldatabase::adddatabase ("Qsqlite", connectionname); Db.setdatabasename ("/jyxtec.db"); if (Db.open ()) return true; else return false;} void Testquery () { qsqlquery query (qsqldatabase::d atabase ("SQLite")); Query.exec ("SELECT * from T_test"); }
Testquery () Here is not supported for multithreaded calls and can only be used in threads that call OpenDatabase (). Otherwise it is easy to segment the error.
There are two ways to resolve this:
1) Create a qsqldatabase of different connectionname in each thread that calls Testquery
such as thread A
Qsqldatabase::adddatabase ("Qsqlite", "A");
Qsqlquery query (qsqldatabase::d atabase ("A"));
Thread B
Qsqldatabase::adddatabase ("Qsqlite", "B");
Qsqlquery query (qsqldatabase::d atabase ("B"));
2) Implement a database thread pool, create n different ConnectionName qsqldatabase, and all of the query commands are placed in this thread pool for processing.
Thanks online Daniel
Reference Address:
http://blog.csdn.net/goldenhawking/article/details/10811409
Http://blog.chinaunix.net/uid-20680966-id-4779621.html
QT Multithreading access to database problems