Results 1 to 3 of 3

Thread: Qt4 QPSQL QSqlQuery::lastInsertId oid problem

  1. #1
    Join Date
    Aug 2010
    Posts
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Question Qt4 QPSQL QSqlQuery::lastInsertId oid problem

    My application use different database drivers, to connect. And i want to use one code for different database (now i am testing Oracel OCI and PSQL). And i have troubles with psql driver:

    After insert query, i use QSqlQuery::lastInsertId() method to get id of inserted row. LastInsertId return oid value, not id, that i am waiting. I use additional query (something like sequence::cur_val) or make query like "SELECT id FROM table_name WHERE oid= lastInsertId". But, this is crutches around psql driver lastInsertId. How i can get actually id of row? It is not good, when in code i use
    Qt Code:
    1. if (psql_driver) { get_real_id_from_oid(table_name, oid) }
    To copy to clipboard, switch view to plain text mode 
    .

    I found in qt4.6.3 psql driver sources such code for lastInsertId:
    Qt Code:
    1. QVariant QPSQLResult::lastInsertId() const
    2. {
    3. if (isActive()) {
    4. Oid id = PQoidValue(d->result);
    5. if (id != InvalidOid)
    6. return QVariant(id);
    7. }
    8. return QVariant();
    9. }
    To copy to clipboard, switch view to plain text mode 
    Maybe it is usefull to make additional query in psql driver and return realyId or make additional method?!?

    for testing i use such table:
    Qt Code:
    1. CREATE TABLE test (
    2. id oid NOT NULL DEFAULT nextval('test_autoin_sequence'::regclass),
    3. "name" text
    4. )
    5. WITH (
    6. OIDS=TRUE
    7. );
    To copy to clipboard, switch view to plain text mode 
    sequence create:
    Qt Code:
    1. CREATE SEQUENCE test_autoin_sequence
    2. INCREMENT 1
    3. MINVALUE 1
    4. MAXVALUE 9223372036854775807
    5. START 31
    6. CACHE 1;
    To copy to clipboard, switch view to plain text mode 

    thanks

  2. #2
    Join Date
    Apr 2010
    Location
    Minsk, Republic of Belarus
    Posts
    19
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt4 QPSQL QSqlQuery::lastInsertId oid problem

    For pgsql insert query with returning clause:
    INSERT INTO tbl1(col1,col2) VALUES (DEFAULT, 'adsf') RETURNING col1; -- or RETURNING * for all columns
    After that:
    QSqlQuery::next();
    And read returning values (for examle, generated by sequence)
    May be oracle have same mechanism.

  3. #3
    Join Date
    Aug 2010
    Posts
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Cool Re: Qt4 QPSQL QSqlQuery::lastInsertId oid problem

    Thank you for your help, i will try your advice for oracle, postgres and for mysql. If it works (or not) i will write reply

Similar Threads

  1. Problem using the QPSQL plugin on Windows x64 in Debug mode
    By code_talker in forum Qt Programming
    Replies: 4
    Last Post: 12th August 2010, 13:01
  2. Replies: 4
    Last Post: 10th November 2009, 19:48
  3. QPSQL problem
    By LoneWolf in forum Installation and Deployment
    Replies: 60
    Last Post: 4th November 2009, 14:22
  4. QSqlQuery problem
    By MarkoSan in forum Qt Programming
    Replies: 11
    Last Post: 18th December 2007, 13:25
  5. QPSQL plugin problem
    By tuxi in forum Qt Programming
    Replies: 0
    Last Post: 12th March 2007, 20:44

Tags for this Thread

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.