PDA

View Full Version : QSQLQUERY commands



arturs
10th June 2016, 18:48
Hi

I have the following code :


void MainWindow::on_pushButton_clicked()
{

QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setDatabaseName("Driver={Microsoft dBASE Driver (*.dbf)};DriverID=277;Dbq=d:/");
qDebug() << db.open();

qDebug() << db.tables();

QSqlQuery query = QSqlQuery("SELECT * FROM dzialka", db);


query.prepare("INSERT INTO test(P1)"
"VALUES (9999)");
query.exec();

while (query.next()) {
QString test = query.value(0).toString();
qDebug () << test;
}


}


Why If I add the lines :


query.prepare("INSERT INTO test(P1)"
"VALUES (9999)");
query.exec();

The lopp while does not work ? because the "curosor" is set on the last posiotion ?

Regards
Artur

jefftee
10th June 2016, 22:47
Your insert query won't return multiple rows in a result set. In fact, it won't return any, so why do you think the while loop should execute multiple times?

hint: You have a select in the QSqlQuery constructor, but you then later prepare and execute an insert statement on the same QSqlQuery instance.

arturs
11th June 2016, 07:52
Your insert query won't return multiple rows in a result set. In fact, it won't return any, so why do you think the while loop should execute multiple times?

Yes I know. Please take a look once again on my code.
If I remove the following lines from my code (the lines there are before while looop):


query.prepare("INSERT INTO test(P1)"
"VALUES (9999)");
query.exec();

Everything works. All records from test tables are showed but If I add the lines then records from table are not showed.

I would like to know Why ? because "cursor" is set at last record ?

Regards
Artur

ChrisW67
11th June 2016, 08:26
You have already been told. You are trying to iterate over the rows returned through the query object. That last thing that query object executed was an SQL Insert, which returns no rows.

arturs
11th June 2016, 09:06
I have already corrected my post. I used wrong tag by mistake.

I thought that I was over the row but I was not sure.

What should I do to display all records once again ?
Is it necessary to use command SELECT once again or use another way to set "cursor" at first records ?

anda_skoa
11th June 2016, 09:29
What should I do to display all records once again ?

You issue a SELECT query,



Is it necessary to use command SELECT once again or use another way to set "cursor" at first records ?
You are currently not executing any SELECT query.

Cheers,
_

ChrisW67
11th June 2016, 22:16
QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setDatabaseName("Driver={Microsoft dBASE Driver (*.dbf)};DriverID=277;Dbq=d:/");

QSqlQuery insertQry("INSERT INTO test(P1). VALUES (9999)", db); // one query object, prepared and executed.
insertQry.prepare("INSERT INTO test(P1) VALUES(9998);"); // same object reused
insertQry.exec();
insertQry.prepare("INSERT INTO test(P1) VALUES(?);"); // same object reused again
for (int i = 9990; i < 9996: ++i) {
insertQry.bindValue(i);
insertQry.exec();
}

QSqlQuery query = QSqlQuery("SELECT * FROM dzialka", db); // a different query object

while (query.next()) {
QString test = query.value(0).toString();
qDebug () << test;
}

You also need to be aware that an SQL database engine will present results at a consistent point in time. So if you execute a select, insert rows (using a separate query object or external program), and then fetch the results of the select you will generally only see the rows at the time of the select execution. I am not sure if the Microsoft ODBC-dBase bridge maintains that read consistency.