Can't connect to qsqlite plugin



  • On OS with installed QT - everything works. But if QT not installed - i see some bugs...

    Programm can't view plugins and crashed with runtime error if the creation of the base is in a separate class CDatabase. If the content CDatabase :: CDatabase i moved to MainWindow :: MainWindow - everything works. Any ideas?

    OS: Windows XP / Windows 7 x64
    Full source code (failed on OS without QT): http://pastebin.com/6Q2Jtn57
    Full source code (working on all OS): http://pastebin.com/C8KfZ1NB

    mainwindow.cpp
    @
    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "cdatabase.h"

    CDatabase db;

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }
    @

    cdatabase.h
    @
    #ifndef CDATABASE_H
    #define CDATABASE_H

    #include <QtSql/QSqlDatabase>
    #include <QtSql/QSqlError>
    #include <QString>

    class CDatabase
    {
    public:
    CDatabase();
    private:
    QSqlDatabase db;
    };

    #endif // CDATABASE_H
    @

    cdatabase.cpp
    @
    #include "cdatabase.h"

    CDatabase::CDatabase()
    {
    QString connection = QString(QSqlDatabase::defaultConnection);
    if (QSqlDatabase::contains(connection)) {
    db = QSqlDatabase::database(connection);
    } else {
    db = QSqlDatabase::addDatabase("QSQLITE", connection);
    db.setDatabaseName("db.sqlite");
    db.open();
    }
    }
    @

    main.cpp, mainwindow.h, wolqru.pro - not important. For source code see link above.

    List of files in program dir:
    platforms\qwindows.dll
    platforms\qminimal.dll
    sqldrivers\qsqlite.dll
    wolqru.exe
    Qt5Core.dll
    libgcc_s_sjlj-1.dll
    libstdc++-6.dll
    libwinpthread-1.dll
    Qt5Widgets.dll
    Qt5Sql.dll
    Qt5Gui.dll
    libGLESv2.dll
    libEGL.dll
    icuin49.dll
    icuuc49.dll
    icudt49.dll
    D3DCompiler_43.dll


  • Lifetime Qt Champion

    Hi,

    To setup the database you have make an instance of your CDatabase class somewhere (i.e. main.cpp). Just having this class compiled in you executable won't do anything if not used somewhere.

    Hope it helps



  • [quote author="SGaist" date="1365194152"]Hi,

    To setup the database you have make an instance of your CDatabase class somewhere (i.e. main.cpp). [/quote]

    I connect to class in mainwindow.cpp by #include "cdatabase.h". And try to create a new object "CDatabase db;" . May by i something do not understand?


  • Lifetime Qt Champion

    Sorry, i think i've misunderstood what you wrote.

    Could you show the version of the code that is working ?



  • [quote author="SGaist" date="1365196087"]Sorry, i think i've misunderstood what you wrote.
    Could you show the version of the code that is working ?[/quote]

    See link above to view full code of all files.

    1. Create a standart QT Gui application with QT Creator.

    change mainwindow.cpp
    @#include "mainwindow.h"
    #include "ui_mainwindow.h"

    #include <QtSql/QSqlDatabase> // i add this
    #include <QtSql/QSqlError> // i add this
    #include <QString> // i add this
    QSqlDatabase db; // i add this

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    //I add this START

    QString connection = QString(QSqlDatabase::defaultConnection);
    if (QSqlDatabase::contains(connection)) {
        db = QSqlDatabase::database(connection);
    } else {
        db = QSqlDatabase::addDatabase("QSQLITE", connection);
        db.setDatabaseName("db.sqlite");
        db.open();
    }
    

    //I add this END

    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }
    @


  • Lifetime Qt Champion

    For the plugin bug, IIRC, you have to put all plugins in a "plugins" folder besides your application.
    plugins/platform
    /sqldrivers
    etc...

    About your code, since you're using the default connection, you're overcomplicating things:

    @
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName(""db.sqlite"");
    if (!db.open()) {
    QMessageBox::critical(0, qApp->tr("Cannot open database"),
    qApp->tr("Unable to establish a database connection."));
    }
    @

    There is no need to keep a db object, since all other calls to i.e. QSqlQuery will use the default connection.

    This technique is used i.e. by the sql examples in Qt's sources.

    Hope it helps



  • [quote author="SGaist" date="1365197882"]
    There is no need to keep a db object, since all other calls to i.e. QSqlQuery will use the default connection.
    [/quote]
    Thank you. I will use this.

    [quote author="SGaist" date="1365197882"]For the plugin bug, IIRC, you have to put all plugins in a "plugins" folder besides your application.
    plugins/platform
    /sqldrivers
    etc...
    [/quote]
    i copy dll's in plugins folder and it doesn't solve my problem (
    plugins\platforms\qwindows.dll; plugins\sqldrivers\qsqlite.dll

    In failure case Process Explorer always!! show path:
    C:\QT\Qt5.0.1\5.0.1\mingw47_32\plugins\sqldrivers\qsqlite.dll
    C:\QT\Qt5.0.1\5.0.1\mingw47_32\plugins\platforms\qwindows.dll

    In working case Process Explorer show correct path:
    \Device\Mup\vboxsrv\CPP\Projects\wolq-test\release\sqldrivers\qsqlite.dll
    \Device\Mup\vboxsrv\CPP\Projects\wolq-test\release\platforms\qwindows.dll


  • Lifetime Qt Champion

    Have a look at the "deployment documentation":http://qt-project.org/doc/qt-5.0/qtdoc/deployment-windows.html it seems that you are missing some dll's for mingw (also check the plugin directories from there, I think I have mixed something with the Qt4 deployment)



  • I think - it's bug... Can you test my apps on Windows with QT5?

    https://bugreports.qt-project.org/browse/QTBUG-30538 (examples attach there)
    1 program - failure, 2 program - working.

    The difference between these programs is very small.


Log in to reply
 

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