PDA

View Full Version : How is a QSqlQuery bound to a QSqlDatabase?



MTK358
31st July 2010, 16:10
There doesn't seem to be any part that says "this QSqlQuery operates on this QSqlDatabase".

waynew
1st August 2010, 15:06
There are variations on how you can do this, but the basics are:

1. define database
2. define query

Sqlite example:



// define database
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", <yourConnectionName>);
db.setDatabaseName(dbName + ".sqlite");
db.open();

// define query
QSqlQuery query(db); // db ties the query to the defined, open database
query.exec("SELECT * from yourTable");



If you split this code into two pieces then you can use the connectionName and don't have to re-define the database for each query.