[SOLVED] Unable to connect to database..



  • Hi everybody!

    I have been struggling with this database connection problem for two days, and still couldn't find the correct way to fix it. This is the error I keep getting whenever I try to build my project. I know that I have to add QT += sql to my .pro file, but it didn't cure my problem either.

    error LNK2019: unresolved external symbol "__declspec(dllimport) public: bool __thiscall QSqlDatabase::open(void)" (_imp?open@QSqlDatabase@@QAE_NXZ) referenced in function "public: __thiscall MainWindow::MainWindow(class QWidget *)" (??0MainWindow@@QAE@PAVQWidget@@@Z)

    this is my main.cpp:
    @#include "mainwindow.h"
    #include "ui_mainwindow.h"

    #define Path_to_DB ".../Documents/uniquePaper/accounts.sqlite"

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

    myDB = QSqlDatabase::addDatabase("QSQLITE");
    myDB.setDatabaseName(Path_to_DB);
    QFileInfo checkFile(Path_to_DB);
    
    if(checkFile.isFile())
    {
        if(myDB.open())
        {
            ui->result->setText("Connected to the database...");
        }
    
    }else
    {
        ui->result->setText("Database file is not found!...");
    }
    

    }

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

    and this is the header file:

    @#ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include <QDebug>
    #include <QFileInfo>
    #include <QtSql/QSqlDatabase>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    void on_MainWindow_iconSizeChanged(const QSize &iconSize);

    private slots:

    private:
    Ui::MainWindow *ui;
    QSqlDatabase myDB;
    };

    #endif // MAINWINDOW_H
    @

    any suggestions?

    Thanks.



  • Is your library path set correctly?



  • Hey, thanks for the reply! By default, the program was running in debug mode and I changed to release mode which fixed the problem now. I guess it has something to do with sqldrivers plugin.



  • Mmmm...your project settings must be different for debug and release then as it really shouldn't matter otherwise which mode you run in.

    However, if you are happy with your solution, please mark the thread as "Solved" :)


Log in to reply
 

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