The Qt documents state that it is not a best practice to keep the QSqlDatabase
object around.
Here are a few different ways we could go about this:
- We could use
QSqlDatabase::database
to grab an instance of the opened database:
QSqlDatabasedb=QSqlDatabase::database("MAEPQTdb"); QSqlQueryq("",db); q.exec("insertintoMobilevalues(0,'iPhone','6SE','12.1.2')"); q.exec("insertintoMobilevalues(1,'Moto','XT1710-09','2')"); q.exec("insertintoMobilevalues(1,'rpi','1','1')"); q.exec("insertintoMobilevalues(1,'rpi','2','2')"); q.exec("insertintoMobilevalues(1,'rpi','3','3')");
- We can also use another function of
QSqlQuery
, namedprepare()
, which prepares the query string for execution using a proxy variable.
Then, we can use bindValue
to bind the value to its identifier:
q.prepare("insert into Mobile values (id, device, model, version)" "values ( :id, :device, :model, :version)"); q.bindValue(":id", 0); q.bindValue(":device", "iPhone"); q.bindValue(":model", "6SE...