Results 1 to 4 of 4

Thread: problem while executing SQL query

  1. #1
    Join Date
    Jul 2009
    Posts
    36
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default problem while executing SQL query

    hello,

    i'm able to connect with db[MySql].
    i'm trying to Execute the Sql Query but i'm not getting wrong result.

    I want to get the number of row in the current table.

    My Query is :

    Qt Code:
    1. QSqlQuery q1("SELECT COUNT(RecordId) FROM tbl;");
    2. QSqlRecord rec1 = q1.record();
    3. QString Str = q1.value(0).toString();
    4. QMessageBox::information(this, tr("Count"),Str);
    To copy to clipboard, switch view to plain text mode 
    The result in message box is NULL.

    if we run the sql query normally we get the result i.e 10;

    What wrong in the query.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: problem while executing SQL query

    Try:
    Qt Code:
    1. QSqlQuery q1("SELECT COUNT(RecordId) FROM tbl;");
    2. if(q1.next())
    3. qWarning() << "sql-return:" << q1.value(0).toInt();
    To copy to clipboard, switch view to plain text mode 

    EDIT: and use the second parameter for the query constructor to set the database connection, to make sure you querying the right database.

  3. The following user says thank you to Lykurg for this useful post:

    sosanjay (9th October 2009)

  4. #3
    Join Date
    Jul 2007
    Location
    Jundiai/SP, Brazil
    Posts
    114
    Thanks
    5
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: problem while executing SQL query

    Hi

    Try this:

    Note the: exec()

    Qt Code:
    1. QSqlQuery q1("SELECT COUNT(RecordId) FROM tbl;");
    2. q1.exec();
    3. if(q1.next())
    4. qWarning() << "sql-return:" << q1.value(0).toInt();
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: problem while executing SQL query

    Quote Originally Posted by vcp View Post
    Note the: exec()
    You don't need that. The doc says:
    QSqlQuery::QSqlQuery ( const QString & query = QString(), QSqlDatabase db = QSqlDatabase() )
    Constructs a QSqlQuery object using the SQL query and the database db. If db is not specified, the application's default database is used. If query is not an empty string, it will be executed.

Similar Threads

  1. Problem building Firebird sql plugins
    By vieraci in forum Installation and Deployment
    Replies: 11
    Last Post: 12th September 2008, 01:28
  2. INSERT query with MySQL problem
    By timmyg in forum Qt Programming
    Replies: 10
    Last Post: 20th March 2008, 21:52
  3. Problem executing process
    By EricF in forum Qt Programming
    Replies: 4
    Last Post: 25th January 2008, 16:04
  4. SQL problem
    By nimmyj in forum Qt Programming
    Replies: 4
    Last Post: 25th December 2006, 18:43

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.