[SOLVED] Can't insert values to database with prepare and bindValue functions



  • Hello. I've been trying to insert some values to my database. There are three columns in the database and first column is integer primary key autoincrement and the others are integer unique. When I run this code:
    @QSqlQuery query;
    query.exec("insert into tableName values(101, 49, 50)");@

    It adds the values 101,49,50 to the new row. However the following code just increments the first column's value and doesn't add any value to second and third columns:
    @QSqlQuery query;
    int x = 99;
    int y = 51;
    int z = 52;

    query.prepare("INSERT INTO definition(columnName1, columnName2, columnName3)"
    "VALUES (:x, :y, :z)");

    query.bindValue("columnName1", x);
    query.bindValue(":columnName2", y);
    query.bindValue(":columnName3", z);@

    I am using SQLite3 Database and Qt 5.4.0. Also I am wondering if I call the code without adding any value to the first column does the program automatically increments the first column's value and add other values to the second and third columns? Since I couldn't run the code above I couldn't try this. What I am trying to say is a code like this:
    @int y = 51;
    int z = 52;

    query.prepare("INSERT INTO definition(columnName2, columnName3)"
    "VALUES (:y, :z)");

    query.bindValue(":columnName2", y);
    query.bindValue(":columnName3", z);@



  • Try with following

    @query.bindValue(":x", 10);
    query.bindValue(":y", 20);
    query.bindValue(":z", 30);@



  • There is missing colon in the line

    @query.bindValue("columnName1", x);@

    It should be

    @query.bindValue(":columnName1", x);@



  • This solved my problem. Thanks!
    [quote author="Dheerendra" date="1420995958"]Try with following

    @query.bindValue(":x", 10);
    query.bindValue(":y", 20);
    query.bindValue(":z", 30);@
    [/quote]



  • You can close this request chaining the subject line to SOLVED.


Log in to reply
 

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