Results 1 to 10 of 10

Thread: QT5 and SQLLite: Parameter count mismatch upon Insert

  1. #1
    Join Date
    Feb 2013
    Posts
    15
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default QT5 and SQLLite: Parameter count mismatch upon Insert

    I am having issues making my program insert data into an SQLLite db file. I am a bit lost due to the ambigious error message aswell, as I am passing the correct parameters to the insert command(?)

    Here is the SQL Lite Table structure:

    Qt Code:
    1. CREATE TABLE inventory (
    2. id INT PRIMARY KEY NOT NULL,
    3. name CHAR(120) NOT NULL,
    4. description TEXT,
    5. category INT,
    6. employee INT,
    7. location INT
    8. );
    To copy to clipboard, switch view to plain text mode 

    And here is my QT/C++ Code:

    Qt Code:
    1. QSqlQuery query(this->db);
    2.  
    3. if (!this->db.open()) {
    4. qDebug() <<this->db.lastError().text();
    5. }
    6.  
    7. query.prepare
    8. ("INSERT INTO inventory (name, description, category) VALUES (?,?,?)");
    9. query.addBindValue(name);
    10. query.addBindValue(description);
    11. query.addBindValue(category);
    12.  
    13.  
    14.  
    15. if (!query.exec()) {
    16. qDebug() <<query.lastError().text();
    17. }
    To copy to clipboard, switch view to plain text mode 

    For the record, the QSqlDatabase object is initiated in the constructor, and I am able to run SELECT queries from the database. So the connection works ....


    Qt Code:
    1. QSettings settings;
    2. this->db = QSqlDatabase::addDatabase("QSQLITE", "NETTO_CONNECTION");
    3. this->db.setDatabaseName(settings.value("db").toString());
    To copy to clipboard, switch view to plain text mode 

    Any ideas to what I am doing wrong here?

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QT5 and SQLLite: Parameter count mismatch upon Insert

    I don't know about the specific error message, but since "id" is declared not null you need to provide a value on insert.

    BTW. The id column is not an alias for the Sqlite rowid because its data type is not quite right

  3. #3
    Join Date
    Feb 2013
    Posts
    15
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QT5 and SQLLite: Parameter count mismatch upon Insert

    Quote Originally Posted by ChrisW67 View Post
    I don't know about the specific error message, but since "id" is declared not null you need to provide a value on insert.

    BTW. The id column is not an alias for the Sqlite rowid because its data type is not quite right
    Hmm, I thought that auto incremented as I read in the SQLLITE docs that the primary key is aliased to rowid?

    Anyway, I did try to set the "ID" column manually, but still no dice ....

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QT5 and SQLLite: Parameter count mismatch upon Insert

    The column type must be "INTEGER" in full to get he magic meaning.
    http://www.sqlite.org/faq.html#q1

    Is the object "query" constructed using the connection "NETTO_CONNECTION" or is it trying to use the default connection?

  5. #5
    Join Date
    Feb 2013
    Posts
    15
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QT5 and SQLLite: Parameter count mismatch upon Insert

    Quote Originally Posted by ChrisW67 View Post
    The column type must be "INTEGER" in full to get he magic meaning.
    http://www.sqlite.org/faq.html#q1
    Alright, I need to stop blindly trusting google and third party sites then, I was following documentation from this site:

    http://www.tutorialspoint.com/sqlite...sert_query.htm

    Quote Originally Posted by ChrisW67 View Post
    Is the object "query" constructed using the connection "NETTO_CONNECTION" or is it trying to use the default connection?
    Changed:

    Qt Code:
    1. QSqlQuery query(this->db);
    To copy to clipboard, switch view to plain text mode 

    To:

    Qt Code:
    1. QSqlQuery query(this->db.database("NETTO_CONNECTION"));
    To copy to clipboard, switch view to plain text mode 

    And the table structure now reads:

    Qt Code:
    1. CREATE TABLE inventory ( id INTEGER PRIMARY KEY NOT NULL UNIQUE, name CHAR(120) NOT NULL, description TEXT, category INTEGER, employee INTEGER, location INTEGER );
    To copy to clipboard, switch view to plain text mode 

    Still no dice, the same output from QSqlQuery via qDebug(): "Parameter count mismatch"

    (I did tell QSqlQuery to use NETTO_CONNECTION on an earlier attempt, but as I got increasingly more frustrated with the problem I reshuffeled the code blindly in a desperate attempt to locate the problem ... but the INTEGER value was new to me, thanks for the heads up on that one!)

  6. #6
    Join Date
    Feb 2013
    Posts
    15
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QT5 and SQLLite: Parameter count mismatch upon Insert

    Allright, revisiting this thread as I have come up with a solution to my problem, and not contributing to a closure when you have found a workaround is bad form. Basically the solution was be more competent. (even though I have no idea why my initial approach didn’t work in the first place, but I digress). I scrapped the notion of running direct queries on the Sqlite database, and switched my entire codebase over to QT’s model view architecture. So where my code previously looked like this:

    Qt Code:
    1. QSqlQuery query(this->db);
    2.  
    3. if (!this->db.open()) {
    4. qDebug() <<this->db.lastError().text();
    5. }
    6.  
    7. query.prepare
    8. ("INSERT INTO inventory (name, description, category) VALUES (?,?,?)");
    9. query.addBindValue(name);
    10. query.addBindValue(description);
    11. query.addBindValue(category);
    12.  
    13.  
    14.  
    15. if (!query.exec()) {
    16. qDebug() <<query.lastError().text();
    17. }
    To copy to clipboard, switch view to plain text mode 
    It now looks like this:

    Qt Code:
    1. QHash<QString, QVariant> tableInsert;
    2.  
    3. tableInsert["name"] = ui->inputName->text();
    4. tableInsert["description"] = ui->inputDescription->toPlainText();
    5. tableInsert["category"] = ui->inputCategory->currentData().toInt();
    6.  
    7. if (!this->inventory->insert(tableInsert)){
    8. qDebug() << this->inventory->getModel().lastError().text();
    9. } else {
    10. ui->inputName->clear();
    11. ui->inputDescription->clear();
    12.  
    13. ui->inputName->setFocus();
    14. }
    To copy to clipboard, switch view to plain text mode 
    The Inventory object has a QSqlTableModel attached to it, it’s “insert” method reads like this:

    Qt Code:
    1. bool Dataset::insert(QHash<QString, QVariant> values) {
    2.  
    3. QHashIterator<QString, QVariant> i(values);
    4. QSqlRecord record = this->model->record();
    5.  
    6. while (i.hasNext()){
    7. i.next();
    8. record.setValue(i.key(), i.value());
    9. }
    10.  
    11. this->model->insertRecord(-1, record);
    12. return this->model->submitAll();
    13. }
    To copy to clipboard, switch view to plain text mode 


    (where the model object is offcourse an instance of QSqlTableModel). This way I not only get more readable code, but I manage to abstract the code into a more logical workflow. Let me know if you have any comments or suggestions for improvement

  7. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QT5 and SQLLite: Parameter count mismatch upon Insert

    This test program mirrors your starting point with the data type corrected:
    Qt Code:
    1. #include <QtCore>
    2. #include <QtSql>
    3.  
    4. int main(int argc, char **argv)
    5. {
    6. QCoreApplication app(argc, argv);
    7.  
    8. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    9. db.setDatabaseName(":memory:");
    10. if (db.open()) {
    11. QSqlQuery query;
    12.  
    13. bool ok = query.exec(
    14. " CREATE TABLE inventory ( "
    15. " id INTEGER PRIMARY KEY NOT NULL, "
    16. " name CHAR(120) NOT NULL, "
    17. " description TEXT, "
    18. " category INT, "
    19. " employee INT, "
    20. " location INT ); ");
    21. qDebug() << "Create" << ok;
    22.  
    23. ok = query.prepare("INSERT INTO inventory (name, description, category) VALUES (?,?,?)");
    24. qDebug() << "Prepare" << ok;
    25.  
    26. query.addBindValue("name");
    27. query.addBindValue("description");
    28. query.addBindValue(1);
    29.  
    30. ok = query.exec();
    31. qDebug() << "Insert" << ok << query.lastError();
    32.  
    33. if (query.exec("select id from inventory")) {
    34. query.next();
    35. qDebug() << "Id" << query.value(0).toLongLong();
    36. }
    37. }
    38. return 0;
    39. }
    To copy to clipboard, switch view to plain text mode 
    and it runs perfectly well when built with Qt 5.3 on Linux:
    Qt Code:
    1. Create true
    2. Prepare true
    3. Insert true QSqlError("", "", "")
    4. Id 1
    To copy to clipboard, switch view to plain text mode 

    Assuming your database is persistent I'd check that the pre-existing inventory table is correctly defined.

  8. #8
    Join Date
    Feb 2013
    Posts
    15
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QT5 and SQLLite: Parameter count mismatch upon Insert

    Quote Originally Posted by ChrisW67 View Post
    Assuming your database is persistent I'd check that the pre-existing inventory table is correctly defined.
    I haven't changed the table definition after I switched the method for inserting the data, so I really doubt there is anything wrong with the inventory table. According to SQL browser it is now defined as such:

    Qt Code:
    1. CREATE TABLE `inventory` (
    2. `id` INTEGER NOT NULL UNIQUE,
    3. `name` CHAR(120) NOT NULL,
    4. `description` TEXT,
    5. `category` INTEGER,
    6. `employee` INTEGER,
    7. `location` INTEGER,
    8. PRIMARY KEY(id)
    9. );
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QT5 and SQLLite: Parameter count mismatch upon Insert

    Your definition of the id colum is neither an alias for the automatic rowid nor will it accept a NULL value. Consequently your insert must provide a value for the id column, and it does not.

  10. #10
    Join Date
    Feb 2013
    Posts
    15
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QT5 and SQLLite: Parameter count mismatch upon Insert

    Quote Originally Posted by ChrisW67 View Post
    Your definition of the id colum is neither an alias for the automatic rowid nor will it accept a NULL value. Consequently your insert must provide a value for the id column, and it does not.
    "With one exception noted below, if a rowid table has a primary key that consists of a single column and the declared type of that column is "INTEGER" in any mixture of upper and lower case, then the column becomes an alias for the rowid."

    https://www.sqlite.org/lang_createtable.html

    The 'id' column doesn't accept NULL but it certainly auto increments upon insertion of new row with an automatic value without explicitly providing this...

Similar Threads

  1. Replies: 14
    Last Post: 16th May 2017, 03:51
  2. Replies: 6
    Last Post: 13th June 2014, 06:54
  3. Sql problem - parameter mismatch count
    By Marina K. in forum Qt Programming
    Replies: 1
    Last Post: 20th June 2011, 18:27
  4. Parameter count mismatch in create table statement
    By croscato in forum Qt Programming
    Replies: 5
    Last Post: 4th February 2011, 09:38
  5. QTableView and SqlLite
    By Myrgy in forum Qt Programming
    Replies: 1
    Last Post: 4th October 2009, 14:54

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.