What is problem with qQuery.lastInsertId().toInt() ? why below function return -1 for u32FileId variable ?
-
QSqlError FileLog::InsertData(UINT32 &u32FileId)
{
FUNCTION_IN;
QSqlQuery qQuery;qQuery.prepare("INSERT INTO "+sDBTableName[DBTABLE_FILELOG]+"\ (FilePath,\ FileType,\ FileTagName,\ Comment,\ InsertedDate,\ IsDirty)" "VALUES (?, ?, ?, ?, ?, ?)"); qQuery.addBindValue(sFilePath); qQuery.addBindValue(u8FileType); qQuery.addBindValue(sFileTagName); qQuery.addBindValue(sComment); qQuery.addBindValue(dCurrentDate); qQuery.addBindValue(u8Dirty); if(!pMainApp.ObjDbOperations.ExecuteQuery(qQuery)) return qQuery.lastError(); u32FileId = qQuery.lastInsertId().toInt(); //Get Last Auto Increment ID FUNCTION_OUT; return QSqlError();
}
-
@Qt-embedded-developer According to https://doc.qt.io/qt-5/qsqlquery.html#lastInsertId not every database supports returning last insert id. You should check whether yours does using https://doc.qt.io/qt-5/qsqldriver.html#hasFeature
-
so what i need to do to get the last auto increment id of data base in my case
-
@Qt-embedded-developer Did you check whether your database supports this using https://doc.qt.io/qt-5/qsqldriver.html#hasFeature as I suggested?
-
@Qt-embedded-developer If it returns -1 is the new row actually inserted?
-
@Qt-embedded-developer said in What is problem with qQuery.lastInsertId().toInt() ? why below function return -1 for u32FileId variable ?:
no new row not inserted but realtional data base table contains new row which has -1
Sorry, I don't understand this.
If you execute the INSERT query - is there new row in your table afterwards or not?"//Get Last Auto Increment ID" - do you have an autoincrement id column in that table?
-
Qt embedded developer about 3 hours ago
QSqlError FileLog::InsertData(UINT32 &u32FileId)
{
FUNCTION_IN;
QSqlQuery qQuery;qQuery.prepare("INSERT INTO "+sDBTableName[DBTABLE_FILELOG]+"
(FilePath,
FileType,
FileTagName,
Comment,
InsertedDate,
IsDirty)"
"VALUES (?, ?, ?, ?, ?, ?)");
qQuery.addBindValue(sFilePath);
qQuery.addBindValue(u8FileType);
qQuery.addBindValue(sFileTagName);
qQuery.addBindValue(sComment);
qQuery.addBindValue(dCurrentDate);
qQuery.addBindValue(u8Dirty);if(!pMainApp.ObjDbOperations.ExecuteQuery(qQuery))
return qQuery.lastError();u32FileId = qQuery.lastInsertId().toInt(); //Get Last Auto Increment ID
FUNCTION_OUT;
return QSqlError();}
above function is called by below function
void Camera::MakeFileEntry(QString sFilePath, CAPTURED_FILETYPE eFileType)
{
FileLog *ObjFileLog = NULL;
ObjFileLog = new FileLog();
if(IS_VALID_OBJ(ObjFileLog))
{
ObjFileLog->sFilePath = sFilePath;
ObjFileLog->u8FileType = eFileType;
ObjFileLog->dCurrentDate = QDate::currentDate();
ObjFileLog->u8Dirty = DIRTYBIT_SET;ObjFileLog->InsertData(u32CamFileId); DELETE_OBJ(ObjFileLog); }
}
now we have used this u32CamFileId to insert in readinglog table
ObjReadingLog->u32FileId = u32CamFileId;
why above code return -1 for u32CamFileId ?
-
@Qt-embedded-developer said in What is problem with qQuery.lastInsertId().toInt() ? why below function return -1 for u32FileId variable ?:
why above code return -1 for u32CamFileId ?
If you do not answer my questions I cannot help you...
-
@Qt-embedded-developer said in What is problem with qQuery.lastInsertId().toInt() ? why below function return -1 for u32FileId variable ?:
why above code return -1 for u32CamFileId ?
I guess because nothing was inserted?
"no new row inserted in file log"