QSqlQuery Update doesn't work
-
Hi,
I use the following code to save a change made in a QStandardItemModel to a db:model->setData (index,dateEdit->date ().toString ("MM/dd/yyyy")); QVariant d(index.data(Qt::EditRole)); qDebug() << "QVariant: " << d; QString modDate; modDate = d.toString (); qDebug() << "modDate: " << modDate; fixquery.prepare("UPDATE Items SET AdoptDate = :AdoptDate WHERE ID = :fixID"); fixquery.bindValue (":AdoptDate", modDate); fixquery.bindValue (":fixID",fixID); fixquery.exec ();
There is no error message, and according debug the code is executed. In output:
modDate: "05/19/2015" which is correct. Any idea why it is not saved in the database? Thank you. -
Hi,
Please add error checking to your code and print the error.
-
You should print the text of lastError.
-
Print the executed query, that will give you some additional clues.
-
I think this one can do it.
QString QSqlQuery::lastQuery() const- Returns the text of the current query being used, or an empty string if there is no current query text.
and maybe
QSqlQuery QSqlQueryModel::query() constso something like
if (model->query())
QString str=model->query()->lastQuery() ?Disclaimer: Didn't check the actual output.
-
I think this one can do it.
QString QSqlQuery::lastQuery() const- Returns the text of the current query being used, or an empty string if there is no current query text.
and maybe
QSqlQuery QSqlQueryModel::query() constso something like
if (model->query())
QString str=model->query()->lastQuery() ?Disclaimer: Didn't check the actual output.
@mrjj
I added the following after fixquery.exec():qDebug() << "Updating date error: " << fixquery.lastError (); QString last; last = fixquery.lastQuery (); qDebug() << "Last query: " << last; break;
I've got the following output in Application Output:
QVariant: QVariant(QString, "11/26/2015")
modDate: "11/26/2015"
Updating date error: QSqlError("", "", "")
Last query: "UPDATE Items SET AdoptDate = :AdoptDate WHERE ID = :fixID"This looks normal to me....
-
Hi @gabor53,
- What does
fixquery.exec();
return?true
orfalse
? - What does
fixquery.numRowsAffected();
return? - What type is
fixID
, how is it set, and what guarantee do you have that the database actually contains a row with that ID?
Cheers.
- What does
-
Hi @gabor53,
- What does
fixquery.exec();
return?true
orfalse
? - What does
fixquery.numRowsAffected();
return? - What type is
fixID
, how is it set, and what guarantee do you have that the database actually contains a row with that ID?
Cheers.
Hi @Paul-Colby,
Last query: "UPDATE Items SET AdoptDate = :AdoptDate WHERE ID = :fixID"
Fixquery.exec() returns: Fixqueryexec is false
Rows affected: -1.fixID is set like this:
QDateEdit *dateEdit = qobject_cast<QDateEdit*>(editor); QModelIndex updateIndex(index.model ()->index(index.row (),0,index.parent ())); QString fixID; QVariant v(updateIndex.data (Qt::DisplayRole)); fixID = v.toString (); qDebug() << "FixID: " << fixID;
FixID is a QString which will give the value to the ID column in the database.The ID column does exist in the db.
- What does
-
Ok... So completely unrelated... Can't have a nicer bug :D
Glad you found out and thanks for sharing.
Since you have it working now, please mark the thread as solved using the "Topic Tools" button so that other forum users may know a solution has been found :)