Unsolved How can I qDebug a QSqlQuery?
-
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"); -
-
@artwaw That doesn't seem to work without a database connection. They're both empty.
-
@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? -
@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.
-
-
@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.... -
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:");
-
@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.