Database cannot add query to sqlite



  • 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_namestring, 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


  • Moderators

    @xhallix said in Database cannot add query to sqlite:

    product_name INTEGER

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



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


  • Moderators

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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.