What is the correct way of QSqlDatabase & QSqlQuery?

后端 未结 3 1123
难免孤独
难免孤独 2020-12-22 22:17

I got confused with the manual , should i work like this:

{
 QSqlDatabase db = QSqlDatabase::addDatabase (...);
 QSqlQuery query (db);
 query.exec (...);
}

         


        
3条回答
  •  梦毁少年i
    2020-12-22 22:44

    QSqlDatabase and QSqlQuery are lightweight wrappers around concrete implementations, so your first example is fine. If you provide a name when adding the connection, or use the default database, then simply writing 'QSqlDatabase db(name)' gives you the database object with very little overhead.

    removeDatabase is equivalent to closing the file (for sqlite) or the connection (for ODBC/MySql/Postgres), so that's typically something you would do at program termination. As the warning says, you must ensure all database and query objects which refer to that database, have already been destroyed, or bad things can happen.

提交回复
热议问题