QSqlQuery Update doesn't work
-
wrote on 26 Nov 2016, 14:26 last edited by
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.
-
wrote on 27 Nov 2016, 04:46 last edited by
@gabor53
When I changefixquery.bindValue (":AdoptDate", modDate);
to
fixquery.bindValue (":AdoptDate", "11/25/2007");
it works correctly and adds the date to the db. Looks like it doesn't like QString modDate I just can't figure why.
-
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.
wrote on 28 Nov 2016, 04:09 last edited by@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....
-
wrote on 28 Nov 2016, 04:18 last edited by
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.
wrote on 28 Nov 2016, 04:38 last edited byHi @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
-
wrote on 1 Dec 2016, 04:43 last edited by
I finally figured out what the problem was. In the switch statement where this query is I made a typo:
instead ofcase 7:
I had
case 7:;
After deleting the ;i t works fine.
-
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 :)
1/14