How can I qDebug a QSqlQuery?
-
wrote on 28 Nov 2019, 11:38 last edited by
Currently I have no connection to my database, but I'd like to check the query I created using query.prepare() and query.bindValue().
So lets say my code is:
QSqlQuery *query = new QSqlQuery(); query->prepare("INSERT INTO tblpeople(`name`, `lastname`, `age`") VALUES (" :name", ":lastname", ":age"); query->bindValue(":name", ui->lineEdit); query->bindValue(":lastname", ui->lineEdit_2); query->bindValue(":age", ui->LineEdit_3);
I already tried :
qDebug() << query;
but that resulted in a hexadecimal number. Does anyone know how I can debug this into (for example):
INSERT INTO tblpeople(
name
,lastname
,age
) VALUES ("jessica", "smith", "21"); -
wrote on 28 Nov 2019, 11:49 last edited by artwaw
-
wrote on 28 Nov 2019, 12:01 last edited by
@artwaw That doesn't seem to work without a database connection. They're both empty.
-
@artwaw That doesn't seem to work without a database connection. They're both empty.
wrote on 28 Nov 2019, 12:06 last edited by@hobbyProgrammer Right. I missed the part when you say you have no connection to DB. I am sorry.
I never tried but would guess that QSqlQuery::boundValues() can be used? -
@hobbyProgrammer Right. I missed the part when you say you have no connection to DB. I am sorry.
I never tried but would guess that QSqlQuery::boundValues() can be used?wrote on 28 Nov 2019, 12:09 last edited by@artwaw nope unfortunately. I think I really need that database connection.. I've been busy with that for almost a week now, so I just wanted to create some code without having to connect to a database, but there's no other way for me to check if the code works properly.
-
wrote on 28 Nov 2019, 12:43 last edited by JonB
@VRonin
Your proposed answer(s) is the nicest, but unfortunately don't think any there will won't work as the OP has no connection and is not executing any queries :) I think all there rely onlastQuery()
&exec()
, or does one not? OP should look through them.... -
wrote on 28 Nov 2019, 13:34 last edited by VRonin
AFAIK
lastQuery()
doesn't need the query to have been executed so it's just a matter of replacing the placeholders with the bound values.If OP still feels it would be better to have a db connection in place you can just use a dummy in-memory SQLite database:
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName(":memory:");
-
AFAIK
lastQuery()
doesn't need the query to have been executed so it's just a matter of replacing the placeholders with the bound values.If OP still feels it would be better to have a db connection in place you can just use a dummy in-memory SQLite database:
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName(":memory:");
wrote on 28 Nov 2019, 13:39 last edited by@VRonin I suggested lastQuery() but OP said it comes out empty:
@hobbyProgrammer said in How can I qDebug a QSqlQuery?:
@artwaw That doesn't seem to work without a database connection. They're both empty.
1/9