Results 1 to 2 of 2

Thread: Sqlite , QVariantList and QSqlError("", "Parameter count mismatch", "")

  1. #1
    Join Date
    Nov 2015
    Posts
    1
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default 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

    Qt Code:
    1. QSqlDatabase mydb = QSqlDatabase::addDatabase("QSQLITE");
    2. mydb.setDatabaseName("SomeDb.sqlite");
    3. if (mydb.open())
    4. {
    5. QString q1 = "CREATE TABLE Info(FileName VARCHAR(50)";
    6. for (int i=1; i< 24; i++)
    7. {
    8. q1 = q1 + ",Col" + QString::number(i) + " VARCHAR(50)";
    9. }
    10. q1 = q1 + ")";
    11. QSqlQuery query(q1);
    12. query.exec();
    13. }
    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.
    Qt Code:
    1. QString line;
    2. // some code here to read and validate the line of text. I printed the QStringlist to console and it looks fine.
    3. QStringList fields;
    4. fields = line.split(",");
    5. QList<QVariant> variantlist;
    6. variantlist << file.fileName();
    7. foreach(QString s, fields)
    8. {
    9. variantlist << s;
    10. }
    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.

    Qt Code:
    1. QSqlQuery q(mydb);
    2. q.prepare("INSERT INTO Info VALUES (?)");
    3. q.addBindValue(variantlist);
    4. qDebug() << "Bound values: " << q.boundValues();
    5. if (!q.execBatch(QSqlQuery::ValuesAsColumns))
    6. 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?
    Last edited by anda_skoa; 21st November 2015 at 15:41.

  2. #2
    Join Date
    Oct 2011
    Posts
    51
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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

Similar Threads

  1. Replies: 14
    Last Post: 16th May 2017, 03:51
  2. QSqlError("", "Parameter count mismatch", "")
    By Alberto7 in forum Newbie
    Replies: 2
    Last Post: 9th October 2015, 22:09
  3. Replies: 3
    Last Post: 15th December 2014, 16:24
  4. Replies: 3
    Last Post: 25th August 2009, 13:03
  5. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 19:05

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.