[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);@ -
You can close this request chaining the subject line to SOLVED.