Results 1 to 8 of 8

Thread: SELECT * > to $array

  1. #1

    Default SELECT * > to $array

    how can i write in qt this?

    PHP:
    Qt Code:
    1. $sql = "SELECT * FROM ..."
    2. ...
    3. while($row = mysql_assoc_array($result)){
    4. $result[] = $row;
    5. }
    6. return $row;
    To copy to clipboard, switch view to plain text mode 

    I think am best way is use Object class (or maybe structs), but i want try something easy (XP).

  2. #2
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    1
    Thanked 29 Times in 27 Posts

    Default Re: SELECT * > to $array

    Qt Code:
    1. QSqlDatabase *db = QSqlDatabase::addDatabase("QPSQL7", "mainConnect");
    2. //set db's credentials
    3. if (!db->open()) {
    4. // show some error message and stuff like that
    5. }
    6.  
    7. QSqlQuery query(db);
    8. QString qstr;
    9. ...
    10. qstr = QString("your query goes here; ");
    11.  
    12. query.exec(qstr);
    13.  
    14. if (!query.first()) {
    15. // in this case no data has been selected
    16. // do what you think you shoul
    17. }
    18.  
    19. do {
    20. // process your data
    21. } while (query.next())
    To copy to clipboard, switch view to plain text mode 

    http://doc.trolltech.com/4.4/qsqlquery.html
    I'm a rebel in the S.D.G.

  3. #3

    Default Re: SELECT * > to $array

    sorry, wrong question...

    i need this
    Qt Code:
    1. something_as_assoc_array_inPHP DB:myDbFunction(){
    2. QSqlDatabase *db = QSqlDatabase::addDatabase("QPSQL7", "mainConnect");
    3. //set db's credentials
    4. if (!db->open()) {
    5. // show some error message and stuff like that
    6. QSqlQuery query(db);
    7. QString qstr;
    8. qstr = QString("your query goes here; ");
    9. query.exec(qstr);
    10.  
    11. if (!query.first()) {
    12. // in this case no data has been selected
    13. // do what you think you shoul
    14. }
    15. do {
    16. something_as_assoc_array_inPHP = query..........
    17. } while (query.next())
    18. delete db;
    19.  
    20. return something_as_assoc_array_inPHP;
    21. }
    22.  
    23.  
    24. code:
    25.  
    26. var = DB:myDbFunction();
    27. qtDebug() << var[0]["id"];
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanked 370 Times in 336 Posts

    Default Re: SELECT * > to $array

    you still need to use QSqlQuery in loop like was written above.
    you can get access to data using by index
    Qt Code:
    1. QVariant QSqlQuery::value ( int index ) const
    To copy to clipboard, switch view to plain text mode 
    or by field name using
    Qt Code:
    1. QSqlRecord QSqlQuery::record () const
    2. QVariant QSqlRecord::value ( const QString & name ) const
    To copy to clipboard, switch view to plain text mode 

    links:
    getting value by index using QSqlQuery
    getting record
    geeting value by index using QSqlRecord
    getting value by name using QSqlRecord

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

    Default Re: SELECT * > to $array

    Can You explain for what ? I don't see any reason for that.

  6. #6
    Join Date
    May 2008
    Posts
    58
    Thanks
    2

    Default Re: SELECT * > to $array

    I don't think there is an easy way to read all records into a Array directly.
    Does anyone has good idea?

  7. #7
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanked 370 Times in 336 Posts

    Default Re: SELECT * > to $array

    why not? you can keep list of pointers to QSqlRecord. why this is bad idea?

  8. #8
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Thanked 370 Times in 336 Posts

    Default Re: SELECT * > to $array

    you can create you own container for returning result in this way. I've just show how to get values from query using existing methods.

Similar Threads

  1. How to get a Multi select combobox
    By calmspeaker in forum Qt Programming
    Replies: 9
    Last Post: 26th August 2008, 05:49
  2. Signals to select Titles?
    By cnbp173 in forum Qwt
    Replies: 1
    Last Post: 23rd May 2008, 09:49
  3. QTableWidget row select
    By nowire75 in forum Newbie
    Replies: 4
    Last Post: 23rd December 2007, 18:59
  4. select a QTreeWidgetItem
    By mattia in forum Newbie
    Replies: 2
    Last Post: 21st December 2007, 11:01
  5. Sql Server cannot retrieve data from select
    By Atomino in forum Qt Programming
    Replies: 10
    Last Post: 7th September 2006, 16:37

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
  •  
Qt is a trademark of The Qt Company.