Results 1 to 6 of 6

Thread: SQL JOIN queries & QSqlRecord objects not storing table names

  1. #1
    Join Date
    Jul 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default SQL JOIN queries & QSqlRecord objects not storing table names

    Hi! I'm trying to do a LEFT JOIN query on two tables in my database, and everything works just fine with the QSqlQuery object. However, I've been using the record() method to get an record object for the current row so that I can access the database columns by name instead of index.

    However, this doesn't work with multiple tables - the table names are stripped out of the column names. So I can't do something like value("table1.column_name") - instead, the column names are mixed with each other.

    What should I do?

  2. #2
    Join Date
    Jun 2010
    Location
    Salatiga, Indonesia
    Posts
    160
    Thanks
    11
    Thanked 32 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    1

    Default Re: SQL JOIN queries & QSqlRecord objects not storing table names

    You can use QSqlQuery::value(int index) to get the value of field index in the current record.

    Qt Code:
    1. query.exec("SELECT ....");
    2. query.next();
    3. qDebug() << query.value(0).toInt() << query.value(1).toString(); // just an example
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jul 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: SQL JOIN queries & QSqlRecord objects not storing table names

    Quote Originally Posted by saa7_go View Post
    You can use QSqlQuery::value(int index) to get the value of field index in the current record.

    Qt Code:
    1. query.exec("SELECT ....");
    2. query.next();
    3. qDebug() << query.value(0).toInt() << query.value(1).toString(); // just an example
    To copy to clipboard, switch view to plain text mode 
    The problem is that I need to use real field names, not indexes.

  4. #4
    Join Date
    Jun 2010
    Location
    Salatiga, Indonesia
    Posts
    160
    Thanks
    11
    Thanked 32 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    1

    Default Re: SQL JOIN queries & QSqlRecord objects not storing table names

    Quote Originally Posted by SweetieCakes View Post
    So I can't do something like value("table1.column_name")...
    Does these codes:
    Qt Code:
    1. qry.exec("SELECT * FROM table1");
    2. qry.next();
    3. qDebug() << qry.record().value("table1.col_name").toString();
    To copy to clipboard, switch view to plain text mode 
    work for you?

    When i tried that, the output from qDebug() is "". But, if i call
    Qt Code:
    1. qDebug() << qry.record().value("col_name").toString();
    To copy to clipboard, switch view to plain text mode 
    the output is correct.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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: SQL JOIN queries & QSqlRecord objects not storing table names

    If you know up front that you'll be making the joins then there should be no problem in creating your own map which will let you access fields by names that will be converted to indexes in the record and fed to QSqlValue::value()
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #6
    Join Date
    Jul 2010
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: SQL JOIN queries & QSqlRecord objects not storing table names

    Quote Originally Posted by wysota View Post
    If you know up front that you'll be making the joins then there should be no problem in creating your own map which will let you access fields by names that will be converted to indexes in the record and fed to QSqlValue::value()
    My temporary solution is to have a QList of QString's (the column names) and building the query using it and then getting the right indexes using indexOf(): However, this turned something that took 10-20 lines of code to double of the original size.

    Is there really no way to make QSqlRecord not forget the table prefixes?

Similar Threads

  1. Replies: 3
    Last Post: 9th January 2010, 15:47
  2. Replies: 9
    Last Post: 19th November 2009, 09:18
  3. Replies: 7
    Last Post: 18th July 2006, 21:33
  4. Table cell merge/join
    By larry104 in forum Qt Programming
    Replies: 6
    Last Post: 16th June 2006, 22:35
  5. QDataTable + join
    By zlatko in forum Qt Programming
    Replies: 1
    Last Post: 27th April 2006, 12:56

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.