SQLite and QSqlQuery "Parameter count mismatch" Error
-
wrote on 17 Dec 2020, 18:45 last edited by ppitu
Hi
I have a function to update record in database:void SummaryPaymentDao::updateSummaryPayment(const SummaryPayment &summaryPayment) const { qDebug() << summaryPayment.getProductName(); QSqlQuery query(mDatabase); query.prepare("UPDATE summary_payment SET (product_name = :product_name, price = :price) WHERE id = (:id)"); query.bindValue(":product_name", summaryPayment.getProductName()); query.bindValue(":price", summaryPayment.getPrice()); query.bindValue(":id", summaryPayment.getId()); query.exec(); qDebug() << query.lastQuery(); DatabaseManager::debugQuery(query); }
But when i call this function i get error:
"Parameter count mismatch"
I tried to look for answers on the forum but no solution worked. -
What Qt version do you use?
Please check the return values of query.prepare() and bindValue(). Also re-write your query toUPATE summary_payment SET product_name = :product_name, price = :price WHERE id = :id
-
wrote on 17 Dec 2020, 19:27 last edited by
I am using Qt 1.15.1
I re-write this line but i have still this problem.
And i check the lastError():
Parameter count mismatch"
And lastQuery():
UPDATE summary_payment SET product_name = :product_name, price = :price WHERE id = :id -
Hi
I have a function to update record in database:void SummaryPaymentDao::updateSummaryPayment(const SummaryPayment &summaryPayment) const { qDebug() << summaryPayment.getProductName(); QSqlQuery query(mDatabase); query.prepare("UPDATE summary_payment SET (product_name = :product_name, price = :price) WHERE id = (:id)"); query.bindValue(":product_name", summaryPayment.getProductName()); query.bindValue(":price", summaryPayment.getPrice()); query.bindValue(":id", summaryPayment.getId()); query.exec(); qDebug() << query.lastQuery(); DatabaseManager::debugQuery(query); }
But when i call this function i get error:
"Parameter count mismatch"
I tried to look for answers on the forum but no solution worked.wrote on 17 Dec 2020, 19:28 last edited by@ppitu
In addition to @Christian-Ehrlicher 's correction to your syntax.SQLite apparently returns "Parameter count mismatch" for "almost any" error :)
-
I am using Qt 1.15.1
I re-write this line but i have still this problem.
And i check the lastError():
Parameter count mismatch"
And lastQuery():
UPDATE summary_payment SET product_name = :product_name, price = :price WHERE id = :idwrote on 17 Dec 2020, 19:30 last edited by JonB@ppitu
What did you do about @Christian-Ehrlicher'sPlease check the return values of query.prepare() and bindValue().
?
We do not know what is in your database. We don't know whether you have a table named
summary_payment
. We don't know whether it has the column names you specify. We don't know whether your code is supplying good values for these. -
Lifetime Qt Championwrote on 17 Dec 2020, 19:33 last edited by Christian Ehrlicher
This works fine for me:
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); db.open(); QSqlQuery query(db); query.exec("CREATE TABLE summary_payment (product_name text, price real, id primary key)"); query.prepare("UPDATE summary_payment SET product_name = :product_name, price = :price WHERE id = :id"); query.bindValue(":product_name", QLatin1String("product_name")); query.bindValue(":price", 8); query.bindValue(":id", 7); if (!query.exec()) { qDebug() << query.lastError().databaseText() << query.lastError().driverText(); }
-
@ppitu
In addition to @Christian-Ehrlicher 's correction to your syntax.SQLite apparently returns "Parameter count mismatch" for "almost any" error :)
wrote on 17 Dec 2020, 19:39 last edited byHere is a link to the whole file.
I create the table the same as @Christian-Ehrlicher , and insert to database work correct i have a problem with update and remove -
Did you try my code? If so adjust yours until it's working as expected.
-
wrote on 17 Dec 2020, 19:53 last edited by
@Christian-Ehrlicher i tried your code (in new project) and it worked fine.
But when i change code in my app I get this error: "" "Parameter count mismatch" -
Then simplify your code until it works or looks like mine to find your problem.
-
wrote on 17 Dec 2020, 20:25 last edited by
Thanks for help, i found my mistake (in database in table summay_payment i have: summarypayment_id not id)
-
Thanks for help, i found my mistake (in database in table summay_payment i have: summarypayment_id not id)
wrote on 18 Dec 2020, 10:07 last edited by JonB@ppitu
Which is why, as I suggested, we could not help you.....You really ought take the time to find one of the various free off-line or on-line SQLite workbench tools available to test your queries outside of Qt while you develop them.
7/12