Results 1 to 5 of 5

Thread: postgreSQL - Last Inserted ID

  1. #1
    Join Date
    Oct 2006
    Posts
    13
    Thanks
    3
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default postgreSQL - Last Inserted ID

    I am using QT4.2.3 with PostgreSQL. I am having a following problem.

    This is how my table looks like...

    [first]

    id (sequence) (int8)
    fname
    lname

    When i did...

    QSqlQuery query;
    query.prepare("INSERT INTO first (fname, lname) VALUES (?, ?)");
    query.addBindValue(QString(Fname));
    query.addBindValue(QString(Lname))
    query.exec();

    I got a record inserted in my first table.
    But is there any way to retrieve the id after inserting the data in the [first], as it is a sequence?

    I have tried using a:

    QVariant variant=query.lastInsertId();
    int i=variant.toInt();

    but I am always getting 0 as a result...
    Doing something wrong?

    When I try a driver feature It says It is OK.
    Last edited by nnidza; 26th March 2007 at 22:09.

  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: postgreSQL - Last Inserted ID

    You can check the current value of the sequence:
    SQL Code:
    1. SELECT currval('first_id_seq')
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Oct 2006
    Posts
    13
    Thanks
    3
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: postgreSQL - Last Inserted ID

    But what if someone make another transaction in the same time?
    I am not an expert but I just thought about it...

  4. #4
    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: postgreSQL - Last Inserted ID

    Quote Originally Posted by nnidza View Post
    But what if someone make another transaction in the same time?
    You'll get the Right(tm) value:
    Return the value most recently obtained by nextval for this sequence in the current session. (An error is reported if nextval has never been called for this sequence in this session.) Notice that because this is returning a session-local value, it gives a predictable answer whether or not other sessions have executed nextval since the current session did.

  5. The following user says thank you to jacek for this useful post:

    nnidza (2nd April 2007)

  6. #5
    Join Date
    Oct 2006
    Posts
    13
    Thanks
    3
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: postgreSQL - Last Inserted ID

    Thank you, it works!

Similar Threads

  1. PostgreSQL plugin under Windows
    By aekilic in forum Newbie
    Replies: 1
    Last Post: 22nd January 2007, 06:43
  2. Replies: 5
    Last Post: 28th August 2006, 14:36
  3. PostgreSQL and Windows Community Version
    By graeme in forum Installation and Deployment
    Replies: 23
    Last Post: 20th April 2006, 19:29
  4. QT4 and Postgresql driver
    By alphaqt in forum Installation and Deployment
    Replies: 6
    Last Post: 27th February 2006, 15:50

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.