Though your query is highly unconventional the problem arises because the query is executed twice.
Once in the constructor and once with the exec().
This is how to execute just once:
Q_CHECK_PTR(driver);
/* Here I fill the record with values */
QString smt
= driver
->sqlStatement
(QSqlDriver::InsertStatement, tableName, record,
false);
if (!query.exec(smt))
{
QMessageBox::critical(this, qApp
->applicationName
(), tr
("Error inserting into database. %1").
arg(query.
lastError().
text()));
return;
}
QSqlDriver *driver = QSqlDatabase::database().driver();
Q_CHECK_PTR(driver);
QSqlRecord record = driver->record(tableName);
/* Here I fill the record with values */
QString smt = driver->sqlStatement(QSqlDriver::InsertStatement, tableName, record, false);
QSqlQuery query;
if (!query.exec(smt))
{
QMessageBox::critical(this, qApp->applicationName(), tr("Error inserting into database. %1").arg(query.lastError().text()));
return;
}
To copy to clipboard, switch view to plain text mode
or
if (!query.isValid()) .......
QSqlQuery query(smt);
if (!query.isValid()) .......
To copy to clipboard, switch view to plain text mode
Bookmarks