Sqlite , QVariantList and QSqlError("", "Parameter count mismatch", "")
I am trying to write to a Sqlite db using the execBatch(QSqlQuery::ValuesAsColumns)) method in order to bind a Qvariantlist of string to the query. I end up getting the dreaded QSqlError("", "Parameter count mismatch", "").
I have searched and tried all the tips even though most of them were not related to QVariantlist issues.
I delete the database each time i run the code and recreate it inside Qt with the following code to give me 24 columns
Code:
mydb.setDatabaseName("SomeDb.sqlite");
if (mydb.open())
{
QString q1
= "CREATE TABLE Info(FileName VARCHAR(50)";
for (int i=1; i< 24; i++)
{
q1
= q1
+ ",Col" + QString::number(i
) + " VARCHAR(50)";
}
q1 = q1 + ")";
query.exec();
}
The creation "looks" okay since I can find the db in firefox db manager with the columns as planned it.
I then read in a bunch of strings into a QStringList and then convert the QStringList to a QVariantList.
Code:
// some code here to read and validate the line of text. I printed the QStringlist to console and it looks fine.
fields = line.split(",");
QList<QVariant> variantlist;
variantlist << file.fileName();
{
variantlist << s;
}
At this point I should have the QVariantlist populated with the correct data and checking the size results in 24 entries (as expected).
So then I create a new sqlquery to insert the row into the table, each item in the variant list is for a specific column.
Code:
q.prepare("INSERT INTO Info VALUES (?)");
q.addBindValue(variantlist);
qDebug() << "Bound values: " << q.boundValues();
if (!q.
execBatch(QSqlQuery::ValuesAsColumns)) qDebug() << q.lastError();
and the result of this is as follow:
items count in variant list 24
Bound values: QMap((":a", QVariant(QVariantList, (QVariant(QString, "../../data/rws.txt"), QVariant(QString, ".451"), QVariant(QString, " 231"), QVariant(QString, " GECO VMR"), QVariant(QString, "231.48"), QVariant(QString, ".661"), QVariant(QString, ""), QVariant(QString, ""), QVariant(QString, ""), QVariant(QString, ""), QVariant(QString, ""), QVariant(QString, ".451"), QVariant(QString, ".141"), QVariant(QString, ".141"), QVariant(QString, ""), QVariant(QString, ""), QVariant(QString, ""), QVariant(QString, ""), QVariant(QString, ""), QVariant(QString, ""), QVariant(QString, ""), QVariant(QString, ""), QVariant(QString, ""), QVariant(QString, "15")))))
QSqlError("", "Parameter count mismatch", "")
Any ideas?
Re: Sqlite , QVariantList and QSqlError("", "Parameter count mismatch", "")
I didn't use QSql for a long time, but I think that addBindValue adds whole list as ONE parameter, and what you want to do is add every value from list to query. Try add every value from your list to query using simple for loop and see what will happen :)