Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. MS Access and QSqlQuery
Forum Updated to NodeBB v4.3 + New Features

MS Access and QSqlQuery

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 3.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • UndeadBlowU Offline
    UndeadBlowU Offline
    UndeadBlow
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      Do you get an error back from your queryInfo? Check the lastError() method.

      1 Reply Last reply
      0
      • UndeadBlowU Offline
        UndeadBlowU Offline
        UndeadBlow
        wrote on last edited by
        #3

        [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(

        1 Reply Last reply
        0
        • C Offline
          C Offline
          ChrisW67
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • UndeadBlowU Offline
            UndeadBlowU Offline
            UndeadBlow
            wrote on last edited by
            #5

            [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!

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved