Important: Please read the Qt Code of Conduct -

[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) :
    ui(new Ui::MainWindow)

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


    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

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

    private slots:

    Ui::MainWindow *ui;
    QSqlDatabase myDB;

    #endif // MAINWINDOW_H

    any suggestions?


  • 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