Results 1 to 10 of 10

Thread: how to get integers from mysql

  1. #1
    Join Date
    Nov 2006
    Posts
    96

    Default how to get integers from mysql

    Hi, I have this sql statement;

    Qt Code:
    1. QSqlQuery query;
    2. query.exec(trUtf8("SELECT id FROM descriptor_table WHERE ime_tabele='%1'").arg("town"));
    To copy to clipboard, switch view to plain text mode 

    where descriptor_table and "town" are two tables in my database. The descriptor_table hold info about other tables.

    Now I want to read the rows into the application at which the certain table appears. In this case lines are 1,2,3 (if I enter this command in mysql> command line). The problem now is that I want to get them into an application...

    Any ideas are appreciated.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to get integers from mysql


  3. #3
    Join Date
    Nov 2006
    Posts
    96

    Default Re: how to get integers from mysql

    Why isn't this working:

    Qt Code:
    1. QSqlQuery query;
    2. QVariant integer[m_pTableRecord->count()];
    3. query.prepare(trUtf8("SELECT id FROM descriptor_table WHERE ime_tabele='%1'").arg(sTableName));
    4. for(qint32 i=0;i<m_pTableRecord->count();i++) {
    5. integer[i] = query.value(0);
    6. query.next();
    7. qDebug() << "Value " << i << "is: " << integer[i].toInt();
    8. }
    To copy to clipboard, switch view to plain text mode 

    It always returns 0. It should return 0,1,2.

  4. #4
    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: how to get integers from mysql

    The query is prepared, but I don't see any bindValue() or exec()...

    Edit: Take a look at http://doc.trolltech.com/4.3/qsqlque...binding-values and you'll see what I mean.
    J-P Nurmi

  5. #5
    Join Date
    Nov 2006
    Posts
    96

    Default Re: how to get integers from mysql

    And why doesn't this get the QString that's in table descriptor_table under field browse_caption at id==<some_number>

    Qt Code:
    1. QVariant string[m_pTableRecord->count()];
    2. for (m_iIndex=1; m_iIndex<m_pTableRecord->count(); m_iIndex++) {
    3. query.prepare(trUtf8("SELECT browse_caption FROM descriptor_table WHERE id='%1'").arg(integer[m_iIndex+1].toInt()));
    4. query.exec();
    5. string[m_iIndex] = query.value(0);
    6. qDebug() << "String " << string[m_iIndex].toString();
    7. m_pColumnNames->append(string[m_iIndex].toString());
    8. }
    To copy to clipboard, switch view to plain text mode 

    What it outputs is empty. Why
    Last edited by eleanor; 7th November 2007 at 23:24.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to get integers from mysql

    You have to prepare and execute the query only once. Next, to get the data, you have to first invoke next() and then value().

  7. #7
    Join Date
    Nov 2006
    Posts
    96

    Default Re: how to get integers from mysql

    Qt Code:
    1. query.prepare(trUtf8("SELECT browse_caption FROM descriptor_table WHERE id='%1'").arg(integer[m_iIndex+1].toInt()));
    To copy to clipboard, switch view to plain text mode 

    Well I can't prepare the statement only once, because I have to change the argument to the prepend) function. The argument is an integer that should raise by one every time I go through for loop.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to get integers from mysql

    Quote Originally Posted by eleanor View Post
    Well I can't prepare the statement only once, because I have to change the argument to the prepend) function.
    I haven't noticed that you have changed the query. In such case you can prepare the statement only once and then use bindValue() to change the parameter value.

    Qt Code:
    1. q.prepare( "SELECT ... FROM ... WHERE id = :id" );
    2. for( ... ) {
    3. q.bindValue( ":id", nextId );
    4. if( q.exec() && q.next() ) {
    5. // ...
    6. }
    7. else {
    8. // exec failed or no data
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by eleanor View Post
    The argument is an integer that should raise by one every time I go through for loop.
    Then maybe you don't need the WHERE clause at all?

  9. #9
    Join Date
    Nov 2006
    Posts
    96

    Default Re: how to get integers from mysql

    Hi.

    Qt Code:
    1. query.prepare(trUtf8("SELECT browse_caption FROM descriptor_table WHERE id=':id'"));
    2.  
    3. for (m_iIndex=0; m_iIndex<m_pTableRecord->count(); m_iIndex++) {
    4. query.bindValue(":id",(integer[m_iIndex].toInt()));
    5. query.exec();
    6. string[m_iIndex] = query.value(0);
    7. query.next();
    8. qDebug() << "String " << string[m_iIndex].toString();
    9. m_pColumnNames->insert(m_iIndex, string[m_iIndex].toString());
    10. }
    To copy to clipboard, switch view to plain text mode 

    This code does now work (it does not return a string...actually it always returns an empty string. --> what to do)?
    Last edited by eleanor; 8th November 2007 at 16:10.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: how to get integers from mysql

    Quote Originally Posted by eleanor View Post
    "SELECT browse_caption FROM descriptor_table WHERE id=':id'"
    It should be :id, not ':id'.

    Quote Originally Posted by eleanor View Post
    string[m_iIndex] = query.value(0);
    query.next();
    You have to call next() first, then value().

Similar Threads

  1. mysql configuration with qt
    By bala in forum Installation and Deployment
    Replies: 3
    Last Post: 6th November 2007, 11:02
  2. qt4.3.1 mysql problem
    By twells55555 in forum Qt Programming
    Replies: 5
    Last Post: 8th October 2007, 21:26
  3. Qt 4.2.3 with MSVC 2005 and MySQL
    By whitefurrows in forum Qt Programming
    Replies: 4
    Last Post: 3rd May 2007, 16:32
  4. MySQL starting problem
    By shamik in forum General Discussion
    Replies: 5
    Last Post: 25th April 2007, 07:20
  5. Qt 4.1.4 & Mysql 5 on Linux x64
    By bothapn in forum Installation and Deployment
    Replies: 7
    Last Post: 4th August 2006, 13: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.