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
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();
}
QSqlDatabase mydb = QSqlDatabase::addDatabase("QSQLITE");
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 + ")";
QSqlQuery query(q1);
query.exec();
}
To copy to clipboard, switch view to plain text mode
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.
// 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;
}
QString line;
// some code here to read and validate the line of text. I printed the QStringlist to console and it looks fine.
QStringList fields;
fields = line.split(",");
QList<QVariant> variantlist;
variantlist << file.fileName();
foreach(QString s, fields)
{
variantlist << s;
}
To copy to clipboard, switch view to plain text mode
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.
q.prepare("INSERT INTO Info VALUES (?)");
q.addBindValue(variantlist);
qDebug() << "Bound values: " << q.boundValues();
if (!q.
execBatch(QSqlQuery::ValuesAsColumns)) qDebug() << q.lastError();
QSqlQuery q(mydb);
q.prepare("INSERT INTO Info VALUES (?)");
q.addBindValue(variantlist);
qDebug() << "Bound values: " << q.boundValues();
if (!q.execBatch(QSqlQuery::ValuesAsColumns))
qDebug() << q.lastError();
To copy to clipboard, switch view to plain text mode
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?
Bookmarks