QT SQLite Multi-database operation Daquan one. Single Database model
General Qt is to open a default database connection, operation of a database connection, but the frequency changes in SQLite is easy to lock, so there is a design pattern is to change the frequency of the table in different database files, but to modify the code to manipulate the database part
Usually open code http://developer.nokia.com/community/wiki/Creating_an_SQLite_database_in_Qt
BOOL Databasemanager:: Opendb() {//Find qslite driverDb=Qsqldatabase:: Adddatabase("Qsqlite");#ifdefQ_os_linux//Note:we has to store database file into user home folder in LinuxQString Path (qdir:: Home().Path ()); Path.Append (Qdir:: Separator()).Append"My.db.sqlite"); Path=Qdir:: Tonativeseparators(path); Db.Setdatabasename (path);#else //Note:file exists in the application private folder, in Symbian Qt implementationDb.Setdatabasename ("My.db.sqlite");#endif //Open Databasee returnDb.Open (); }qsqlerror Databasemanager:: LastError() {//If Opening database has failed user can ask //Error description by Qsqlerror::text () returnDb.LastError (); }bool Databasemanager::d eletedb() {//Close databaseDb.Close ();#ifdefQ_os_linux//Note:we has to store database file into user home folder in LinuxQString Path (qdir:: Home().Path ()); Path.Append (Qdir:: Separator()).Append"My.db.sqlite"); Path=Qdir:: Tonativeseparators(path);returnQFile:: Remove(path);#else //Remove created database binary file returnQFile:: Remove("My.db.sqlite");#endif}
This is the most important thing is two steps
Qsqldatabase db = Qsqldatabase::adddatabase ("Qsqlite"); #打开数据库
Db.setdatabasename ("My.db.sqlite"); #指定数据库文件
But repeated calls to Qsqldatabase::adddatabase ("Qsqlite"); Will error when
Qsqldatabaseprivate::adddatabase:duplicate connection name ' qt_sql_default_connection ', old connection removed.
Two. Open multiple databases at the same time
QT itself is support, only the general wording used less. The successful code is as follows, generally requiring a global array to hold these objects
="base.db"if(QSqlDatabase::contains(dbName)){ //如已经打开这个数据库,直接调出这个数据连接 = QSqlDatabase::database(dbName); }else//否则打开这个数据库,注意带上数据库名 = QSqlDatabase::addDatabase("QSQLITE",dbName);
Three. class invocation of the related database
Because of the introduction of multiple databases, the corresponding class calls are introduced into the database connection object.
3.1 Qsqlquery
SQL query class, you can execute various SQL directives. The default method of use is
QSqlQueryquery(); query.exec("select * from table1");
But this situation, in the multi-database mode will be error, hint, >qsqlquery::exec:database not open
In fact, this is the result of calling the default data connection and not opening it, which can be initialized as a direct database object
QSqlDatabase db ; //已经打开数据库对象 QSqlQueryquery(db); query.exec("select * from table1");
3.2 Qsqlquerymodel Class
This is also a commonly used class, with a variety of controls quite useful,
It specifies that the database connection is made when calling Setquery,
//已经打开数据库对象*=new QSqlQueryModel(); sqlModel->setQuery("select * from table",db);
QT SQLite Multi-database operation Daquan