Hi guys:
I'm having a problem with QSqlQuery. I'm trying to insert a record to a database, like this:
Q_CHECK_PTR(driver);
/* Here I fill the record with values */
QString smt
= driver
->sqlStatement
(QSqlDriver::InsertStatement, tableName, record,
false);
if (!query.exec())
{
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(smt);
if (!query.exec())
{
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
Ok, the problem is that I get always a duplicate entry error: "Duplicate entry '47552' for key 'id' QMYSQL: Unable the execute query", but I'm sure that value doesn't exist. In fact, the record is inserted when query.exec() is executed (but I get the error anyways). The number differs everytime in +1 ('cause I'm getting it with a max(id)+1 query).
The primary key isn't autoincremental.
Any ideas?
Thanks!!
Bookmarks