Insert data from variables to Database
-
Good morning.
I am working on a project and while i am able to send numbers to my MYSQL database with this code:
QSqlQuery query; query.prepare("INSERT INTO vathmoi (V_1) " "VALUES (10)"); query.exec(); // this runs it
i can not send data using that code
QString b; b=10; QSqlQuery query; query.prepare("INSERT INTO vathmoi (V_1) " "VALUES (b)"); query.exec(); // this runs it
So i dont know how to handle that isue cause in the project i am developing all that wich are going to be stored in the db are in variables.
Thank you in advance.
-
You can reference variables in your quere using
bindValue(...)
:query.prepare("INSERT INTO vathmoi (V_1) VALUES (:myValue)"); query.bindValue(":myValue", b); query.exec(); // ....
See also these examples: http://doc.qt.io/qt-5/sql-sqlstatements.html#inserting-updating-and-deleting-records
-
Hi and good morning :)
Your syntax still looks wrong. ( use syntax as @micland shows)
and please also use
if(!query.exec()) {
qDebug() << "SQL Statement Error" << query.lastError();
}
as @the_
mentions in
https://forum.qt.io/topic/68149/connect-mysql-database-to-qt-app/4 -
Just a note:
QString b;
b=10;
qDebug() << b;Does NOT give u "10" but ascii 10
b=65
would give u "A"QString b;
b=QString::number(10);Give you "10".