MS Access and QSqlQuery
-
Hi everyone! My english is not very good but i'll try to describe my problem.
So, i have primitive code:
@base = QSqlDatabase::addDatabase("QODBC");QSettings sets("FlowModel","Settings");
currentBase = sets.value("currentBase").toString();base.setHostName("localhost"); base.setDatabaseName(QString("DRIVER={Microsoft Access Driver (*.mdb)};FIL={MS Access};DBQ=%1").arg(currentBase));
if(base.open())
QMessageBox::information(0,"Все отлично!","База данных открыта","Ок");else
QMessageBox::information(0,"Все не ахти!",base.lastError().text(),"Ок");QSqlQuery queryMaterials("SELECT * FROM Материал",base);
int fieldNo = queryMaterials.record().indexOf("Название");
int i = 0;
while (queryMaterials.next()) {
comboBox->insertItem(i++,queryMaterials.value(fieldNo).toString());
}
queryMaterials.clear();@
It works correctly and combo box takes all materials from Database;
But next is going this code:
@QSqlQuery queryInfo("SELECT * FROM Свойства_материала WHERE Название='Вода'",base);fieldNo = queryInfo.record().indexOf("P");
pLine->setText(queryInfo.value(fieldNo).toString());@
And it didn't work! Query returns an empty string (""), but must be a number. I test this SQL-query in Access and there it works correct. Please help to understand what a problem i have.
Thank you. -
[quote author="Andre" date="1351454878"]Do you get an error back from your queryInfo? Check the lastError() method.[/quote]
Thank you for your answer. Yes, I'm tried to use this method, but it report me nothing.
I can't understand what is that... Because this table can be opened by this code:
@QSqlDatabase accessBase = QSqlDatabase::addDatabase("QODBC");accessBase.setHostName("localhost");
accessBase.setDatabaseName("DRIVER={Microsoft Access Driver (*.mdb)};FIL={MS Access};DBQ=D:/ИТ.mdb");
if(accessBase.open())
QMessageBox::information(0,"Все отлично!","База данных открыта","Ок");else
QMessageBox::information(0,"Все не ахти!",accessBase.lastError().text(),"Ок");QTableView tableGhost;
QSqlTableModel tableDB;
QString whtpn = QInputDialog::getText(0, "Какую таблицу открыть?",
"Какую таблицу открыть?");tableDB.setTable(whtpn);
tableDB.select();
tableDB.setEditStrategy(QSqlTableModel::OnFieldChange);
tableGhost.setModel(&tableDB);
tableGhost.show();@
And all ok. But by query no( -
When you QSqlQuery::exec() (as the constructor does), the newly active() query is not pointing at a valid record. You need to call next() as the examples in the docs do.
From the Detailed section of the QSqlQuery docs:
bq. Successfully executed SQL statements set the query's state to active so that isActive() returns true. Otherwise the query's state is set to inactive. In either case, when executing a new SQL statement, the query is positioned on an invalid record. An active query must be navigated to a valid record (so that isValid() returns true) before values can be retrieved.
-
[quote author="ChrisW67" date="1351471417"]When you QSqlQuery::exec() (as the constructor does), the newly active() query is not pointing at a valid record. You need to call next() as the examples in the docs do.
From the Detailed section of the QSqlQuery docs:
bq. Successfully executed SQL statements set the query's state to active so that isActive() returns true. Otherwise the query's state is set to inactive. In either case, when executing a new SQL statement, the query is positioned on an invalid record. An active query must be navigated to a valid record (so that isValid() returns true) before values can be retrieved.
[/quote]Heh, thank you very much! Now all works. I was careless :)
Good luck and thanks again!