Results 1 to 4 of 4

Thread: cannot connect to QSqldatabase

  1. #1
    Join Date
    May 2008
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default cannot connect to QSqldatabase

    hey all,

    I'm using Qt4.4.1 and trying to connect to mysql database using the following code:
    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
    2. db.setHostName("acidalia");
    3. db.setDatabaseName("customdb");
    4. db.setUserName("mojito");
    5. db.setPassword("J0a1m8");
    6. bool ok = db.open();
    To copy to clipboard, switch view to plain text mode 

    i also include
    Qt Code:
    1. #include <QtSql>
    2. #include <QSqlDatabase>
    To copy to clipboard, switch view to plain text mode 

    but i get the following error:

    Qt Code:
    1. src\mainwindowimpl.cpp: In member function `void MainWindowImpl::fileNew()':
    2. src\mainwindowimpl.cpp:124: error: `QSqlDatabase' undeclared (first use this function)
    3. src\mainwindowimpl.cpp:124: error: (Each undeclared identifier is reported only once for each function it appears in.)
    4. src\mainwindowimpl.cpp:124: error: expected `;' before "db"
    5. src\mainwindowimpl.cpp:125: error: `db' undeclared (first use this function)
    6. src\mainwindowimpl.cpp:129: warning: unused variable 'ok'
    7. mingw32-make.exe[1]: *** [build/mainwindowimpl.o] Error 1
    8. e:\qt\MinGW\bin\mingw32-make.exe: *** [release] Error 2
    To copy to clipboard, switch view to plain text mode 

    i know it's been answered before in this forum here: http://www.qtcentre.org/forum/f-newb...blem-5861.html

    but i followed the solution and yet it didn't work for me.

    Any ideas!?

    thanks in advance.

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: cannot connect to QSqldatabase

    did you added
    Qt Code:
    1. QT += sql
    To copy to clipboard, switch view to plain text mode 
    in your .pro file?

  3. #3
    Join Date
    May 2008
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: cannot connect to QSqldatabase

    well, this issue is fixed now.

    another thing is, the plugin issue, i get this msg when i run the executable:

    Qt Code:
    1. QSqlDatabase: QMYSQL driver not loaded
    2.  
    3. QSqlDatabase: available drivers: QSQLITE QODBC3 QODBC
    To copy to clipboard, switch view to plain text mode 

    there is a solution which said the following:

    1. you need to compile qt with -qt-sql-mysql -lmysql -I
    c:\path\to\mysqlsrc\include -L c:\path\to\msyqlinstall\lib\opt
    As on http://qtnode.net/wiki/Qt4_on_Windows
    is this true? Do i need to compile qt from source with these arguments in order to get sql to work?

    btw i have 4 dll files in Qt\4.4.1\plugins\sqldrivers which are:

    1.qsqlite4.dll
    2.qsqlited4
    3.qsqlodbc4.dll
    4.qsqlodbc4d.dll

    which i copied to my Qt\4.4.1\bin

    thanks.

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: cannot connect to QSqldatabase

    Quote Originally Posted by mave-rick View Post
    there is a solution which said the following:



    is this true? Do i need to compile qt from source with these arguments in order to get sql to work?
    yup, you need to compile mysql plugin.

    Quote Originally Posted by mave-rick View Post
    btw i have 4 dll files in Qt\4.4.1\plugins\sqldrivers which are:

    1.qsqlite4.dll
    2.qsqlited4
    3.qsqlodbc4.dll
    4.qsqlodbc4d.dll

    which i copied to my Qt\4.4.1\bin
    you should't do this, Qt find this dll by itself.

Similar Threads

  1. QSqlDatabase connectivity issues
    By ToddAtWSU in forum Qt Programming
    Replies: 2
    Last Post: 16th July 2008, 12:58
  2. QSqlDatabase Mysql timeout versus PortScanner
    By patrik08 in forum Qt Programming
    Replies: 1
    Last Post: 25th May 2008, 00:08
  3. GCC can't find QSqlDatabase!!
    By brevleq in forum Qt Programming
    Replies: 5
    Last Post: 29th October 2007, 07:05
  4. qsqldatabase does not sense db disconnect
    By rburge in forum Qt Programming
    Replies: 0
    Last Post: 9th March 2006, 18:59
  5. connect to sql server
    By raphaelf in forum Qt Programming
    Replies: 15
    Last Post: 27th February 2006, 18:06

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.