Results 1 to 5 of 5

Thread: database connection

  1. #1
    Join Date
    Mar 2008
    Location
    Morocco
    Posts
    47
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default database connection

    Hi..
    I have a trouble to connect to a mysql databse!!
    I used a simple function to estblish connection in the main method :


    Qt Code:
    1. bool createConnection(){
    2. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    3. db.setDatabaseName("projet_cpp");
    4. db.setPassword("passwrd");
    5. db.setHostName("localhost");
    6. if(!db.open()) {
    7. QMessageBox::critical(0, qApp->tr("Cannot open database"),
    8. qApp->tr("Unable to establish a database connection.\n"
    9. "Click Cancel to exit."), QMessageBox::Cancel);
    10. return false;
    11. }
    12. QMessageBox::critical(0, qApp->tr(" open database"),
    13. qApp->tr("Goood It Works.\n"
    14. "Click ."), QMessageBox::Cancel);
    15. return true;
    16. }
    17.  
    18.  
    19.  
    20. int main(int argc,char *argv[]){
    21. QApplication app(argc,argv);
    22. if (!createConnection())
    23. return 1;
    24. ..
    25. ..
    26. }
    To copy to clipboard, switch view to plain text mode 

    after execution I get always the messageBox telling that conexion was failled..

    I read in Qt assistant that I should add the following include:
    #include "C:\Qt\4.3.4\src\sql\drivers\mysql\qsql_mysql. cpp"

    but it gives only errors !!

    I tried :#include "C:\Qt\4.3.4\src\sql\drivers\mysql\qsql_mysql. h"
    but it gives the error :mysql.h no such file or directory within qsql_mysql.h!!!


    if someone could help !!
    thanks
    Last edited by jpn; 12th May 2008 at 20:07. Reason: missing [code] tags

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: database connection

    J-P Nurmi

  3. #3
    Join Date
    Oct 2007
    Location
    Cracow
    Posts
    56
    Thanks
    1
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: database connection

    you forgot add
    Qt Code:
    1. setUserName ( const QString & name )
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Mar 2008
    Location
    Morocco
    Posts
    47
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: database connection

    hi..
    I tried :
    cd %QTDIR%\src\plugins\sqldrivers\mysql
    qmake -o Makefile "INCLUDEPATH+=C:\MySQL\include" "LIBS+=C:\MYSQL\\lib\opt\libmysql.lib" mysql.pro
    make


    bu it gives the error: mysql.h no such file or directory ?
    had installed mysql including libs

    fo the usrName I don t think I will need it .. coze Im workin; as an admnistration d I didnt add Other users n Mysql

    /?????

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: database connection

    Yes, you need to install MYSQL to be able to build the QMYSQL plugin. You can find instructions for MinGW in our wiki: [WIKI]Building the QMYSQL plugin on Windows using MinGW[/WIKI]
    J-P Nurmi

Similar Threads

  1. Threads and database connection
    By probine in forum Qt Programming
    Replies: 9
    Last Post: 7th August 2013, 08:30
  2. Replies: 4
    Last Post: 14th April 2008, 16:33
  3. Multiple database connections
    By cyberboy in forum Qt Programming
    Replies: 3
    Last Post: 30th March 2008, 16:56
  4. Client/Server Error: BadIDChoice
    By 3nc31 in forum Qt Programming
    Replies: 5
    Last Post: 27th November 2007, 10:22
  5. Qt and MySQL Database Connection
    By shamik in forum Qt Programming
    Replies: 41
    Last Post: 6th October 2006, 12:48

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.