Results 1 to 7 of 7

Thread: QT MySQL

  1. #1
    Join Date
    Jan 2007
    Posts
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default QT MySQL

    Hello,
    I am a new commer in QT. I am using QT 3.3.4, MYSQL 4.1.11 and OS is Linux (Fedora Core 4 ). I wand to create a simple programe in QT ( Linux -Fedora Core 4 Version ). I designed a form and wite the code. The programe is working. After that I had try to connect this programe with MYSQL, then it is not working, How can I connect my programme with MYSQL?
    I give some lines in my programme, that the following,

    Qt Code:
    1. #include <qapplication.h>
    2. #include "FarkData.h"
    3. #include <qsqldatabase.h>
    4.  
    5. int main( int argc, char ** argv )
    6. {
    7. QApplication a( argc, argv );
    8. QsqlDatabase *defaultDB=QsqlDatabase::addDatabase(“QMYSQL3”);
    9. defaultDB->setDatabaseName(“test”);
    10. defaultDB->setPassword(" “);
    11. defaultDB->setHostName(“localhost”);
    12.  
    13. if(defaultDB->open())
    14. {
    15. qWarning(“Database connection successful”);
    16. }
    17. else
    18. {
    19. qWarning(“Database connection failed”);
    20. return a.exec();
    21. }
    22.  
    23. MarkList w;
    24. w.show();
    25. a.connect( &a, SIGNAL( lastWindowClosed() ), &a, SLOT( quit() ) );
    26. return a.exec();
    27. }
    To copy to clipboard, switch view to plain text mode 

    after this code i give a commend,
    qmake -o Makefile mark.pro
    Then no error displayed, then i give the command,
    make
    Then a list of error is display, the error is

    /usr/lib/qt-3.3/bin/uic FarkData.ui -o .ui/FarkData.h
    g++ -c -pipe -Wall -W -O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -m32 -march=i386 -mtune=pentium4 -fasynchronous-unwind-tables -DQT_NO_DEBUG -DQT_SHARED -DQT_THREAD_SUPPORT -I/usr/lib/qt-3.3/mkspecs/default -I. -I/usr/lib/qt-3.3/include -I.ui/ -I. -I.moc/ -o .obj/main.o main.cpp
    main.cpp:9: error: stray ‘\226’ in program
    main.cpp:9: error: stray ‘\128’ in program
    main.cpp:9: error: stray ‘\156’ in program
    main.cpp:9: error: stray ‘\226’ in program
    main.cpp:9: error: stray ‘\128’ in program
    main.cpp:9: error: stray ‘\157’ in program
    main.cpp:11: error: stray ‘\226’ in program
    main.cpp:11: error: stray ‘\128’ in program
    main.cpp:11: error: stray ‘\156’ in program
    main.cpp:11: error: stray ‘\226’ in program
    main.cpp:11: error: stray ‘\128’ in program
    main.cpp:11: error: stray ‘\157’ in program
    main.cpp:12: error: stray ‘\226’ in program
    main.cpp:12: error: stray ‘\128’ in program
    main.cpp:12: error: stray ‘\156’ in program
    main.cpp:12: error: stray ‘\226’ in program
    main.cpp:12: error: stray ‘\128’ in program
    main.cpp:12: error: stray ‘\156’ in program
    main.cpp:13: error: stray ‘\226’ in program
    main.cpp:13: error: stray ‘\128’ in program
    main.cpp:13: error: stray ‘\156’ in program
    main.cpp:13: error: stray ‘\226’ in program
    main.cpp:13: error: stray ‘\128’ in program
    main.cpp:13: error: stray ‘\157’ in program
    main.cpp:17: error: stray ‘\226’ in program
    main.cpp:17: error: stray ‘\128’ in program
    main.cpp:17: error: stray ‘\156’ in program
    main.cpp:17: error: stray ‘\226’ in program
    main.cpp:17: error: stray ‘\128’ in program
    main.cpp:17: error: stray ‘\157’ in program
    main.cpp:21: error: stray ‘\226’ in program
    main.cpp:21: error: stray ‘\128’ in program
    main.cpp:21: error: stray ‘\156’ in program
    main.cpp:21: error: stray ‘\226’ in program
    main.cpp:21: error: stray ‘\128’ in program
    main.cpp:21: error: stray ‘\157’ in program
    .ui/FarkData.h:47: error: ISO C++ forbids declaration of ‘QSqlDatabase’ with no type
    /usr/lib/qt-3.3/include/qsqldatabase.h:63: warning: ‘class QSqlDriverCreatorBase’ has virtual functions but non-virtual destructor
    main.cpp: In function ‘int main(int, char**)’:
    main.cpp:9: error: ‘QsqlDatabase’ was not declared in this scope
    main.cpp:9: error: ‘defaultDB’ was not declared in this scope
    main.cpp:9: error: ‘QsqlDatabase’ is not a class or namespace
    main.cpp:9: error: ‘QMYSQL3’ was not declared in this scope
    main.cpp:9: error: ‘addDatabase’ was not declared in this scope
    main.cpp:11: error: ‘test’ was not declared in this scope
    main.cpp:13: error: ‘localhost’ was not declared in this scope
    main.cpp:17: error: ‘Database’ was not declared in this scope
    main.cpp:21: error: ‘Database’ was not declared in this scope
    make: *** [.obj/main.o] Error 1
    What to do ?
    please help me...........

    Sabeesh
    Last edited by wysota; 10th January 2007 at 11:47. Reason: reformatted to look better

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QT MySQL

    First thing to do would probably be to get rid of the stray characters - did you copy and paste the code into your program from some other source (the docs, Internet)? This often causes stray characters to appear. Check the quotes (they are certainly not regular ' " ' characters in the code above), etc.

  3. #3
    Join Date
    Jan 2007
    Posts
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QT MySQL

    Hi,
    Thakyou for your reply, I had take a new project, after that i had add three LineEdit control and three pushbuttons. After that i write the code for working that. then it is worked. After that i wish to insert that values into a table ( MYSQL ). Then i get into the net and get the code. I paste that code in main.cpp and give the command for make.
    I had replace the special character ( ie, ->, , et ). After this i give the command " qmake -o Makefile mark.pro " then there is no error display.
    But after that i give the command "make" then the error message is display
    The code is
    =================================
    QsqlDatabase *defaultDB=QsqlDatabase::addDatabase("QMYSQL3");
    defaultDB->setDatabaseName("test");
    defaultDB->setPassword("");
    defaultDB->setHostName("localhost");
    if(defaultDB->open())
    {
    qWarning("Database connection successful");
    }
    else
    {
    qWarning("Database connection failed");
    return a.exec();
    }

    =====================

    The error is
    ==============================
    g++ -c -pipe -Wall -W -O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -m32 -march=i386 -mtune=pentium4 -fasynchronous-unwind-tables -DQT_NO_DEBUG -DQT_SHARED -DQT_THREAD_SUPPORT -I/usr/lib/qt-3.3/mkspecs/default -I. -I/usr/lib/qt-3.3/include -I.ui/ -I. -I.moc/ -o .obj/main.o main.cpp
    main.cpp:6:28: error: connection.h: No such file or directory
    .ui/FarkData.h:47: error: ISO C++ forbids declaration of ‘QSqlDatabase’ with no type
    /usr/lib/qt-3.3/include/qsqldatabase.h:63: warning: ‘class QSqlDriverCreatorBase’ has virtual functions but non-virtual destructor
    main.cpp: In function ‘int main(int, char**)’:
    main.cpp:12: error: ‘QsqlDatabase’ was not declared in this scope
    main.cpp:12: error: ‘defaultDB’ was not declared in this scope
    main.cpp:12: error: ‘QsqlDatabase’ is not a class or namespace
    main.cpp:12: error: ‘addDatabase’ was not declared in this scope
    make: *** [.obj/main.o] Error 1
    ================================================== =====


    After this I try for another code and get a new one, that is

    ==================================

    QSqlDatabase *defaultDB = QSqlDatabase::addDatabase( "QMYSQL" );
    defaultDB->setDatabaseName( "test" );
    defaultDB->setUserName( "root" );
    defaultDB->setPassword( "" );
    defaultDB->setHostName( "localhost");

    if ( defaultDB->open() ) {
    printf("Sucessfully Connected \n");
    // Database successfully opened; we can now issue SQL commands.
    }

    ===================================

    then i give the command
    qmake -o Makefile mark.pro
    no error display... When i give the command
    make

    then the following error display...
    ====================================
    /usr/lib/qt-3.3/bin/uic FarkData.ui -o .ui/FarkData.h
    g++ -c -pipe -Wall -W -O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -m32 -march=i386 -mtune=pentium4 -fasynchronous-unwind-tables -DQT_NO_DEBUG -DQT_SHARED -DQT_THREAD_SUPPORT -I/usr/lib/qt-3.3/mkspecs/default -I. -I/usr/lib/qt-3.3/include -I.ui/ -I. -I.moc/ -o .obj/main.o main.cpp
    main.cpp:6:28: error: connection.h: No such file or directory
    .ui/FarkData.h:47: error: ISO C++ forbids declaration of ‘QSqlDatabase’ with no type
    /usr/lib/qt-3.3/include/qsqldatabase.h:63: warning: ‘class QSqlDriverCreatorBase’ has virtual functions but non-virtual destructor
    make: *** [.obj/main.o] Error 1
    ==================================

    please help me sir....
    thankyou...
    Sabeesh
    Last edited by sabeeshcs; 10th January 2007 at 12:52.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QT MySQL

    Did you #include <qsqldatabase.h>?

  5. #5
    Join Date
    Jan 2007
    Posts
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QT MySQL

    Yes, I include these header files,

    #include <qapplication.h>
    #include "/usr/include/mysql/mysql.h"
    #include <qsqldatabase.h>
    #include <qsqlquery.h>

    ok i solve the problem.


    But now I had occure another one

    I wand to execute a query in QT.
    The following is the code...

    ================================================== ==
    double id, mark1, mark2, mark3, total;
    const char *name;
    name = TxtName->text();
    mark1 = TxtMark1->text().toDouble();
    mark2 = TxtMark2->text().toDouble();
    mark3 = TxtMark3->text().toDouble();
    id = TxtMark4->text().toDouble();

    total = mark1 +mark2+mark3;

    QSqlQuery target;

    QString sql = "insert into student ( s_id, s_name, s_mark1, s_mark2, s_mark3, s_total ) values( " ;

    ==================================================

    After this i am stuck!!!!!!!!!!!
    how can i add the values of the variables to that string ' sql ' ?

    please help me......

    sabeesh




    ================================================== ===






    Sabeesh
    Last edited by sabeeshcs; 11th January 2007 at 07:11.

  6. #6
    Join Date
    Jan 2006
    Location
    Ukraine,Lviv
    Posts
    454
    Thanks
    9
    Thanked 27 Times in 27 Posts
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: QT MySQL

    Just example from docs...
    Qt Code:
    1. QSqlQuery query;
    2. query.prepare( "INSERT INTO atable (id, forename, surname) "
    3. "VALUES (:id, :forename, :surname)" );
    4. query.bindValue( ":id", 1001 );
    5. query.bindValue( ":forename", "Bart" );
    6. query.bindValue( ":surname", "Simpson" );
    7. query.exec();
    To copy to clipboard, switch view to plain text mode 

    p.s. Read this all !
    a life without programming is like an empty bottle

  7. #7
    Join Date
    Jan 2007
    Posts
    4
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QT MySQL

    Thankyou for your help...

    I wish to consentrate developing graphical application in QT. ie, I wish to create an object with the help of some messure. Can you give some documentation about that.

Similar Threads

  1. Deploying with MySQL support under Windows
    By KShots in forum Installation and Deployment
    Replies: 1
    Last Post: 12th October 2006, 10:19
  2. Qt and MySQL Database Connection
    By shamik in forum Qt Programming
    Replies: 41
    Last Post: 6th October 2006, 13:48
  3. MYSQL Triggers??
    By allensr in forum General Programming
    Replies: 1
    Last Post: 2nd October 2006, 22:54
  4. Qt 4.1.4 & Mysql 5 on Linux x64
    By bothapn in forum Installation and Deployment
    Replies: 7
    Last Post: 4th August 2006, 14:23

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.