Database driven application builds without errors, but resulting screen is blank.



  • I have adjusted the tables example that comes with Qt 4.7.4, which uses sqlite. In the example, however, the data is hard-coded into the app, which is of course of no use if you need to build an application that can connect and update the application's db regularly.
    I must be doing something wrong, but I just don't see where.
    My code in .cpp file:
    @#include <QtGui>
    #include <QtSql>

    #include "myCon.h"
    #include <QSqlQuery>

    void initializeModel(QSqlTableModel *model)
    {
    model->setTable("wines");
    model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    model->select();

    model->setHeaderData(0, Qt::Horizontal, QObject::tr("id"));
    model->setHeaderData(1, Qt::Horizontal, QObject::tr("name"));
    model->setHeaderData(2, Qt::Horizontal, QObject::tr("winemaker_id"));
    

    }

    QTableView *createView(QSqlTableModel *model, const QString &title = "")
    {
    QTableView *view = new QTableView;
    view->setModel(model);
    #if defined(Q_OS_SYMBIAN) || defined(Q_WS_MAEMO_5) || defined(Q_WS_SIMULATOR)
    Q_UNUSED(title)
    #else
    view->setWindowTitle(title);
    #endif
    return view;
    }

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);
    if (!createConnection())
    return 1;

    QSqlTableModel model;
    
    initializeModel(&model);
    

    #if defined(Q_OS_SYMBIAN) || defined(Q_WS_MAEMO_5) || defined(Q_WS_SIMULATOR)
    QTabWidget *tabWidget = new QTabWidget;
    tabWidget->addTab(createView(&model), "View 1");
    tabWidget->addTab(createView(&model), "View 2");

    tabWidget->showMaximized();
    

    #else
    QTableView *view1 = createView(&model, QObject::tr("Table Model (View 1)"));
    QTableView *view2 = createView(&model, QObject::tr("Table Model (View 2)"));

    view1->show();
    view2->move(view1->x() + view1->width() + 20, view1->y());
    view2->show();
    

    #endif

    return app.exec&#40;&#41;;
    

    }
    @
    and the header file:
    @
    #ifndef CONNECTION_H
    #define CONNECTION_H

    #include <QMessageBox>
    #include <QSqlDatabase>
    #include <QSqlError>
    #include <QSqlQuery>

    /*
    This file defines a helper function to open a connection to an
    in-memory SQLITE database and to create a test table.

    If you want to use another database, simply modify the code
    below. All the examples in this directory use this function to
    connect to a database.
    

    */
    //! [0]
    static bool createConnection()
    {
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("wyne.sqlite");
    if (!db.open()) {
    QMessageBox::critical(0, qApp->tr("Cannot open database"),
    qApp->tr("Unable to establish a database connection.\n"
    "This example needs SQLite support. Please read "
    "the Qt SQL driver documentation for information how "
    "to build it.\n\n"
    "Click Cancel to exit."), QMessageBox::Cancel);
    return false;
    }

    QSqlQuery query;
    query.exec("select id, name, winemaker_id FROM wines"&#41;;
    
    
    
    return true;
    

    }
    //! [0]

    #endif
    @

    I would appreciate some help on this. From the very deepest bottom of my hart - still have a lot of work to do on this app and the deadline is next week.



  • Your database does not contain any tables nor is it populated with any data. Therefor there is nothing for the view to display.
    @
    static bool createConnection()
    {
    // ...

    QSqlQuery query;
    qDebug() << query.exec&#40;"create table wines(id integer, name text, winemaker_id integer&#41;"&#41;;
    qDebug() << query.exec&#40;"insert into wines values(1, 'one', 0&#41;"&#41;;
    qDebug() << query.exec&#40;"insert into wines values(2, 'two', 0&#41;"&#41;;
    qDebug() << query.exec&#40;"insert into wines values(3, 'three', 0&#41;"&#41;;
    
    return true;
    

    }
    @



  • Check what the query @"select id, name, winemaker_id FROM wines"@ gives as result when executed on the database directly.



  • Hi
    in sqlite manager it displays all the records, and if I save that file to a svg file it comes out like this:
    1,"fiffi","hond"
    2,"betsie","kat"
    3,"gerrie","wolf"
    4,"lettie","hond"
    5,"wagner","wolf"

    Which are all the records.
    So there is nothing wrong with the sql statement or the db, it seems.



  • [quote author="geytjie" date="1316661056"]
    @
    query.exec("select id, name, winemaker_id FROM wines");
    @
    [/quote]
    Is this query successful?



  • [quote author="Lukas Geyer" date="1316669649"]Your database does not contain any tables nor is it populated with any data. Therefor there is nothing for the view to display.
    @
    static bool createConnection()
    {
    // ...

    QSqlQuery query;
    qDebug() << query.exec("create table wines(id integer, name text, winemaker_id integer&#41;"&#41;;
    qDebug(&#41; << query.exec("insert into wines values(1, 'one', 0&#41;"&#41;;
    qDebug(&#41; << query.exec("insert into wines values(2, 'two', 0&#41;"&#41;;
    qDebug(&#41; << query.exec("insert into wines values(3, 'three', 0&#41;"&#41;;
    
    return true;
    

    }
    @[/quote]

    Pardon my ignorance - should I add this in my .cpp file? I have the .pro (which, as I understand it, puts everything toghether), the .cpp file (which does the work, I think), .h which calls the different components, as I understand. I think I get a little confused how to use what and where. :-(



  • I haven't yet showed my .pro file, here is the code:

    @
    HEADERS = myCon.h
    SOURCES = myTablemodel.cpp
    QT += sql
    #install
    target.path = $$[QT_INSTALL_EXAMPLES]/sql/myTablemodel
    sources.files = $$SOURCES * .h $$RESOURCES $$FORMS myTablemodel.pro
    INSTALLS += target sources

    symbian: include($$PWD/../../symbianpkgrules.pri)
    maemo5: include($$PWD/../../maemo5pkgrules.pri)

    OTHER_FILES +=
    qtc_packaging/debian_harmattan/rules
    qtc_packaging/debian_harmattan/README
    qtc_packaging/debian_harmattan/control
    qtc_packaging/debian_harmattan/compat
    qtc_packaging/debian_harmattan/changelog
    QMAKE_MAC_SDK = /Developer/SDKs/MacOSX10.6.sdk
    @



  • The snippet I have posted just creates a table and inserts some data to see if the UI code works as expected (and it does).

    For further diagnosis of your problem please alter your example the following way
    @
    #ifndef CONNECTION_H
    #define CONNECTION_H

    #include <QMessageBox>
    #include <QSqlDatabase>
    #include <QSqlError>
    #include <QSqlQuery>
    #include <QtDebug> // <-- HERE

    /*
    This file defines a helper function to open a connection to an
    in-memory SQLITE database and to create a test table.

    If you want to use another database, simply modify the code
    below. All the examples in this directory use this function to
    connect to a database.
    

    */
    //! [0]
    static bool createConnection()
    {
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("wyne.sqlite");
    if (!db.open()) {
    QMessageBox::critical(0, qApp->tr("Cannot open database"),
    qApp->tr("Unable to establish a database connection.\n"
    "This example needs SQLite support. Please read "
    "the Qt SQL driver documentation for information how "
    "to build it.\n\n"
    "Click Cancel to exit."), QMessageBox::Cancel);
    return false;
    }

    QSqlQuery query;
    qDebug() << query.exec&#40;"select id, name, winemaker_id FROM wines"&#41;; // <-- HERE
    qDebug(&#41; << query.lastError().type() << query.lastError().text(); // <-- HERE
    
    
    return true;
    

    }
    //! [0]
    @
    and provide the debug output.



  • Thanx for helping.

    I did the test with create table - and it showed in the program. But that is obviously not from the db that I'm trying to show the data from.
    I deleted that test code and inserted the error test you suggested - it returned the following in the debug application output window:
    @
    true
    0 " "
    @
    but the program still shows the values we inserted with the test create table. This puzzles me. Does the compiler keep this in the memory?



  • No way the compiler will hard code database data into your application!
    However I don't understand the real problem: the application code is showing you there is no error and that the query (select) works fine. You say that the program shows the values into the database, that is reasonably for the kind of query you have done. Can you explain better what is the problem=



  • I will try to explain it the way my web-based brain figured it out, ok? So please don't get annoyed with me :-)
    I have created a db wyne.sqlite which is going to be hosted somewhere. I have to create an application that can be installed on cellphones with a starting copy of this db on their phone. This app must enable them to search for wines according to certain criteria and, once a week or so, the app must request permission to update the db. When the user clikcs ok, the app makes the connection with the server on-line and updates.
    We also want to give the user a membership to be able to make comments etc.
    Now I am trying to connect to this db (located in the same folder) and extract the data to display to the user, according to the criteria they enter. We don't just want to host a web app that they must connect to everytime it runs.
    It seems that the connection with the db is made ok, but the app displays nothing.



  • Please change your initializeModel method so that it provides a few more debug information, for instance after the model->select() place a debug print of the number of found rows, just to see where the model is behaving oddly:

    @model->select();
    qDebug() << "Found rows " << model->rowCount();
    qDebug() << "Database error " << model->database()->lastError()->text();
    @

    This is just a way to see if there is something wrong with the database connection and/or the query executed from the model. I don't think there is nothing wrong with the model, there must be a problem with the database.
    Have you the opportunity to test the application against another database?



  • Remember I told you I kept on seeing the old test data? Well, I physically deleted the debug and release folders and reran the build process. And now this error came out:

    The test code I added:
    @model->select();
    qDebug() << "Found rows " << model->rowCount();
    qDebug() << "Database error " << model->database()->lastError()->text();
    @

    The error code:
    false
    2 "no such table: wines Unable to execute statement"



  • Well, are you sure you are working on the correct database then? If there is no wyne.sqlite database file in the working directory the SQLITE driver will just create one.

    Use an absulte path for QSqlDatabase::setDatabaseName() to make absoutely sure you are working on the correct database (one which contains your data).



  • Maybe you are not connecting interactively to the right database, or you have more than one in your machine and are doing testing against two different databases....at least now we know that the application is behaving correctly and the database too. The next step is to find out which database is behaving instead of which!



  • I tried changing the db name, but I'm working on a mac and not all the drivers/plug-ins seem to be installed. Have been trying to figure this one out. Also, where can I find a plug-in for Qt 474 so I can create apps for Symbian 3 Nokia phones?



  • Just install the Symbian^3 toolchain using the Qt SDK maintenance utility.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.