Results 1 to 7 of 7

Thread: unable to run query

  1. #1
    Join Date
    Mar 2011
    Posts
    21
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default unable to run query

    i made a simple interface linked to mysql in fedora...................
    it connectted to database...............but when i tried to query using
    QSqlQuery object................
    nothing was returned as output nor i was able to insert anything into database
    plz help
    me

  2. #2
    Join Date
    Oct 2010
    Location
    Belarus
    Posts
    71
    Thanks
    1
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Windows Maemo/MeeGo

    Default Re: unable to run query

    The magic ball is sleeping. Provide us code and errors
    Try read Qt documentation before ask stupid question.

  3. #3
    Join Date
    Mar 2011
    Posts
    21
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: unable to run query

    no errrors were there.......................
    i just printed the message "connected" on connection and while executing the query................
    i looked into my database..............but database was still the same.....no change was there........

    i used q.isactive() for checking the state of query object i created as QSqlQuery q.................
    i used this after connection

    QSqlquery q;
    if(q.isactive())
    cout<<"query complete"
    but nothing was printed
    plz help................

    Quote Originally Posted by unit View Post
    The magic ball is sleeping. Provide us code and errors
    no errrors were there.......................
    i just printed the message "connected" on connection and while executing the query................
    i looked into my database..............but database was still the same.....no change was there........

    i used q.isactive() for checking the state of query object i created as QSqlQuery q.................
    i used this after connection

    QSqlquery q;
    if(q.isactive())
    cout<<"query complete"
    but nothing was printed
    plz help................

  4. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: unable to run query

    Just RTFM. From QSqlQuery::isActive doc :
    An active QSqlQuery is one that has been exec()'d successfully but not yet finished with.
    Where is Yours query exec()'d ???

  5. #5
    Join Date
    Mar 2011
    Posts
    21
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: unable to run query

    Quote Originally Posted by Lesiok View Post
    Just RTFM. From QSqlQuery::isActive doc :
    An active QSqlQuery is one that has been exec()'d successfully but not yet finished with.
    Where is Yours query exec()'d ???
    sorry i forgot..
    i used this also
    after the above code
    q.exec(insert into student values(1,"sachin","m"));
    but no record was added to the database

  6. #6
    Join Date
    Oct 2010
    Location
    Belarus
    Posts
    71
    Thanks
    1
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Windows Maemo/MeeGo

    Default Re: unable to run query

    Qt Code:
    1. qDebug(q.lastError().text().toAscii());
    To copy to clipboard, switch view to plain text mode 

    I think you have mistake in your query string
    Try read Qt documentation before ask stupid question.

  7. The following user says thank you to unit for this useful post:

    sachinmcajnu (11th March 2011)

  8. #7
    Join Date
    Mar 2011
    Posts
    21
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: unable to run query

    Quote Originally Posted by unit View Post
    Qt Code:
    1. qDebug(q.lastError().text().toAscii());
    To copy to clipboard, switch view to plain text mode 

    I think you have mistake in your query string
    #include<iostream>
    #include<QMessageBox>
    #include<QApplication>
    #include<QSqlDatabase>
    #include<QtGui>
    #include <QSqlQuery>
    using namespace std;
    struct QSqlError;
    bool createConnection()
    {
    QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("localhost");
    db.setDatabaseName("test");
    db.setUserName("root");
    db.setPassword("jaisiyaram");
    if (!db.open()) {
    QMessageBox::warning(0, QObject::tr("Database Error"),"sachin");
    return false;
    }

    return true;
    }

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);
    if (!createConnection())
    cout<<"error";
    else
    {
    cout<<"connected";
    return 1;
    }
    QSqlQuery q;
    q.exec("SELECT sno FROM student");

    if (q.isActive())
    QMessageBox::warning(0, "Database Error","SACHIN");


    while (q.next()) {
    int title = q.value(0).toInt();
    //QString year = q.value(1).toString();
    cout<<title << /*": " << qPrintable(year) */ endl;
    }
    //qDebug(q.lastError().text().toAscii());


    /* if (!q.execBatch())
    qDebug() << q.lastError();
    */
    return app.exec();
    }


    it print nothing and on addition of above statemnnt it gave error:
    main.cpp: In function ‘int main(int, char**)’:
    main.cpp:46:20: error: invalid use of incomplete type ‘struct QSqlError’
    /usr/include/QtSql/qsqldatabase.h:57:7: error: forward declaration of ‘struct QSqlError’
    make: *** [main.o] Error 1


    Added after 28 minutes:


    sorry i got the error,..........
    in the else block
    else
    {
    cout<<"connected";
    return 1;
    }

    i was returning 1..........so further statements where not executing
    Last edited by sachinmcajnu; 11th March 2011 at 12:14.

Similar Threads

  1. Query - how to store ?
    By damodharan in forum Qt Programming
    Replies: 2
    Last Post: 31st May 2010, 12:59
  2. Unable to return query using Mysql bindvalue
    By cobaltblue in forum Qt Programming
    Replies: 1
    Last Post: 20th February 2010, 22:29
  3. SQL query
    By JD2000 in forum Newbie
    Replies: 4
    Last Post: 1st December 2009, 14:21
  4. MS SQL Query
    By baray98 in forum General Programming
    Replies: 0
    Last Post: 14th July 2009, 04:25
  5. Unable to fetch data when searching through select query
    By sinha.ashish in forum Qt Programming
    Replies: 3
    Last Post: 10th April 2008, 14: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.