Results 1 to 4 of 4

Thread: QSqlQuery read row id (SOLVED)

  1. #1
    Join Date
    Oct 2008
    Location
    Catalunya
    Posts
    22
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QSqlQuery read row id (SOLVED)

    hello,
    i'm implementing a routine that reads row data from SQLITE db.
    I want to read id and the other fields.
    I can get other fields data correctly, but id data is null.

    my code is:
    Qt Code:
    1. QSqlQuery query("SELECT id,name FROM person");
    2.  
    3. while (query.next())
    4. {
    5. qDebug(query.value(0).toString().toAscii());
    6. qDebug(query.value(1).toString().toAscii());
    7. }
    To copy to clipboard, switch view to plain text mode 

    and result is:
    Qt Code:
    1. <-- nothing
    2. Tom <-- first row name column value.
    To copy to clipboard, switch view to plain text mode 

    Can anybody help me?
    Last edited by josepvr; 18th May 2011 at 13:57. Reason: Solved

  2. #2
    Join Date
    Mar 2011
    Posts
    120
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery read row id

    try
    Qt Code:
    1. int id;
    2. id=query.record().value("id").toInt();
    To copy to clipboard, switch view to plain text mode 

    use #include <QSqlRecord>

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

    josepvr (18th May 2011)

  4. #3
    Join Date
    Oct 2008
    Location
    Catalunya
    Posts
    22
    Thanks
    7
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QSqlQuery read row id

    I've solved the problem!

    problem was creating table, auto increment wasn't set to id field.
    Qt Code:
    1. query.exec("create table person(id INTEGER PRIMARY KEY AUTOINCREMENT,namevarchar(20))");
    To copy to clipboard, switch view to plain text mode 

    Thanks!

  5. #4
    Join Date
    Mar 2011
    Posts
    120
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery read row id

    Without auto increment you can create table .

Similar Threads

  1. Using QSqlQuery
    By darkman_dev in forum Newbie
    Replies: 2
    Last Post: 4th February 2011, 21:40
  2. QSqlQuery
    By yasher in forum Qt Programming
    Replies: 2
    Last Post: 23rd July 2010, 14:25
  3. error with QSqlQuery
    By mmm286 in forum Newbie
    Replies: 5
    Last Post: 26th May 2010, 00:27
  4. QSqlquery
    By codeman in forum Qt Programming
    Replies: 10
    Last Post: 4th June 2009, 12:57
  5. what is going on a QSqlQuery?
    By mismael85 in forum Qt Programming
    Replies: 2
    Last Post: 26th June 2008, 13:35

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.