Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Database cannot add query to sqlite
Qt 6.11 is out! See what's new in the release blog

Database cannot add query to sqlite

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 1.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    xhallix
    wrote on last edited by xhallix
    #1

    I'm trying to add a simple insert statement to my sqlite db.

        QSqlQuery query;
        query.prepare("INSERT INTO products (product_id, product_name, product_price) VALUES (:product_id, :product_name, :product_price)");
        query.bindValue(":product_id", "foo");
        query.bindValue(":product_name", "bar");
        query.bindValue(":product_price", 20);
    

    Opening db works fine, but the above query outputs error: QSqlError("", "Parameter count mismatch", "")

    This is my table schema CREATE TABLE "products" ( entity_idinteger,product_idstring,product_pricefloat,product_name string, PRIMARY KEY(entity_id) )

    When I execute the above statement directly in sqlite, it works.

    Full code

    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("osoms.db");
    
    bool dbOpen = db.open();
    if(!dbOpen) {
        qDebug("Error opening DB");
        exit(1);
    }
    
    QSqlQuery query;
    query.prepare("INSERT INTO products (product_id, product_name, product_price) VALUES (:product_id, :product_name, :product_price)");
    query.bindValue(":product_id", "foo");
    query.bindValue(":product_name", "bar");
    query.bindValue(":product_price", 20);
    
    bool queryOk = query.exec();
    if(!queryOk) {
        qDebug("Error saving product to DB");
        qDebug() << "error:  "<< query.lastError();
        exit(1);
    }
    
    db.close();
    

    Any help will be appreciated

    jsulmJ 1 Reply Last reply
    0
    • X xhallix

      I'm trying to add a simple insert statement to my sqlite db.

          QSqlQuery query;
          query.prepare("INSERT INTO products (product_id, product_name, product_price) VALUES (:product_id, :product_name, :product_price)");
          query.bindValue(":product_id", "foo");
          query.bindValue(":product_name", "bar");
          query.bindValue(":product_price", 20);
      

      Opening db works fine, but the above query outputs error: QSqlError("", "Parameter count mismatch", "")

      This is my table schema CREATE TABLE "products" ( entity_idinteger,product_idstring,product_pricefloat,product_name string, PRIMARY KEY(entity_id) )

      When I execute the above statement directly in sqlite, it works.

      Full code

      QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
      db.setDatabaseName("osoms.db");
      
      bool dbOpen = db.open();
      if(!dbOpen) {
          qDebug("Error opening DB");
          exit(1);
      }
      
      QSqlQuery query;
      query.prepare("INSERT INTO products (product_id, product_name, product_price) VALUES (:product_id, :product_name, :product_price)");
      query.bindValue(":product_id", "foo");
      query.bindValue(":product_name", "bar");
      query.bindValue(":product_price", 20);
      
      bool queryOk = query.exec();
      if(!queryOk) {
          qDebug("Error saving product to DB");
          qDebug() << "error:  "<< query.lastError();
          exit(1);
      }
      
      db.close();
      

      Any help will be appreciated

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @xhallix said in Database cannot add query to sqlite:

      product_name INTEGER

      product_name is declared as INTEGER, but you're passing a string.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3
      • X Offline
        X Offline
        xhallix
        wrote on last edited by xhallix
        #3

        @jsulm your hint did and adding an absolute path did the trick. thanks

        jsulmJ 1 Reply Last reply
        1
        • X xhallix

          @jsulm your hint did and adding an absolute path did the trick. thanks

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @xhallix Check the string returned by http://doc.qt.io/qt-5/qsqlquery.html#executedQuery after executing the query

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          3

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved