QSqlQuery "Parameter count mismatch" when binding with name
-
I am using SQLite and it supports named placeholders. I am currently using named placeholders but in some situation they dont work.
My query that I got error:bool DB::myFunc(const QString &col1txt, const QString &col2txt, const QString &col3txt, const QString &col4txt, const int col5int, const QString &col6txt) { query.prepare("INSERT INTO mytable(col1txt, col2txt, col3txt, col4txt, col5int, col6txt) VALUES(:col1txt, :col2txt, :col3txt, :col4txt, :col5int, :col6txt)"); query.bindValue(":col1txt", col1txt); query.bindValue(":col2txt", col2txt); query.bindValue(":col3txt", col3txt); query.bindValue(":col4txt", col4txt); query.bindValue(":col5int", col5int); query.bindValue(":col6txt", col6txt); if(query.exec()){ return true; } else{ qDebug() << query.lastError().text(); return false; } }What's wrong with it? And anyone have information if there is such situation named placeholers will not work?
-
@JonB No actually i just paste my prepared statement, not all code snippet. i will modify it now.
@masa4
Change (temporarily) to use void QSqlQuery::bindValue(int pos, const QVariant &val, QSql::ParamType paramType = QSql::In) or void QSqlQuery::addBindValue(const QVariant &val, QSql::ParamType paramType = QSql::In) and?placeholders. Does that make any difference?Does
mytableindeed require just these 6 parameters? It might be that you get a "Parameter count mismatch" if you do not have the right number/names of columns, I don't know.Try statement like
query.prepare("INSERT INTO mytable(col1txt, col2txt, col3txt, col4txt, col5int, col6txt) VALUES(\"col1txt\", \"col2txt\", \"col3txt\", \"col4txt\", 5, \"col6txt\")");so no values to bind, does that work?
-
I am using SQLite and it supports named placeholders. I am currently using named placeholders but in some situation they dont work.
My query that I got error:bool DB::myFunc(const QString &col1txt, const QString &col2txt, const QString &col3txt, const QString &col4txt, const int col5int, const QString &col6txt) { query.prepare("INSERT INTO mytable(col1txt, col2txt, col3txt, col4txt, col5int, col6txt) VALUES(:col1txt, :col2txt, :col3txt, :col4txt, :col5int, :col6txt)"); query.bindValue(":col1txt", col1txt); query.bindValue(":col2txt", col2txt); query.bindValue(":col3txt", col3txt); query.bindValue(":col4txt", col4txt); query.bindValue(":col5int", col5int); query.bindValue(":col6txt", col6txt); if(query.exec()){ return true; } else{ qDebug() << query.lastError().text(); return false; } }What's wrong with it? And anyone have information if there is such situation named placeholers will not work?
-
@masa4
So far I see 6 placeholders to be bound but only 1 named value actually bound. So I would expect a "Parameter count mismatch". Though with noexec()I don't see where/when you would get the error your report.... -
@JonB No actually i just paste my prepared statement, not all code snippet. i will modify it now.
@masa4
Change (temporarily) to use void QSqlQuery::bindValue(int pos, const QVariant &val, QSql::ParamType paramType = QSql::In) or void QSqlQuery::addBindValue(const QVariant &val, QSql::ParamType paramType = QSql::In) and?placeholders. Does that make any difference?Does
mytableindeed require just these 6 parameters? It might be that you get a "Parameter count mismatch" if you do not have the right number/names of columns, I don't know.Try statement like
query.prepare("INSERT INTO mytable(col1txt, col2txt, col3txt, col4txt, col5int, col6txt) VALUES(\"col1txt\", \"col2txt\", \"col3txt\", \"col4txt\", 5, \"col6txt\")");so no values to bind, does that work?
-
@masa4
Change (temporarily) to use void QSqlQuery::bindValue(int pos, const QVariant &val, QSql::ParamType paramType = QSql::In) or void QSqlQuery::addBindValue(const QVariant &val, QSql::ParamType paramType = QSql::In) and?placeholders. Does that make any difference?Does
mytableindeed require just these 6 parameters? It might be that you get a "Parameter count mismatch" if you do not have the right number/names of columns, I don't know.Try statement like
query.prepare("INSERT INTO mytable(col1txt, col2txt, col3txt, col4txt, col5int, col6txt) VALUES(\"col1txt\", \"col2txt\", \"col3txt\", \"col4txt\", 5, \"col6txt\")");so no values to bind, does that work?
-
@JonB hmm It still throws same error and when i checked db yes you are right there are just 5 columns. There were 6 actually but i probably merged date and time columns as one column afterward. Thanks for help
-
M masa4 has marked this topic as solved on