QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName("contacts.db"); db.open(); QSqlQuery query(db); query.prepare("SELECT * FROM contacts"); query.exec(); while (query.next()) { // do something with data } query.finish(); db.close();
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName("contacts.db"); db.open(); QSqlQuery query(db); query.prepare("INSERT INTO contacts (name, phone) VALUES (:name, :phone)"); query.bindValue(":name", "John"); query.bindValue(":phone", "555-555-1212"); query.exec(); query.finish(); db.close();In both examples, the QSqlQuery finish method is called after the query has been executed and results have been processed. This releases any resources used by the query and frees memory used by the result set. The QSqlQuery class is part of the QtSql module in the Qt library.