Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    PostgreSql and problem with lastInsertId()

    General and Desktop
    2
    3
    3575
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • H
      Hostel last edited by

      I have a problem with method lastInsertId. I'm using a PostgreSql 8.4 and Qt 4.6.3 under Linux. I have a code like this:
      @
      // _model is a QSqlTableModel
      QSqlRecord record = _model->record();
      _model->insertRecord( -1, record );

      SomeDialog* dialog = new SomeDialog( _model );
      dialog->exec();
      if( dialog->result() == QDialog::Accepted )
      {
          bool ok = _model->submitAll();
          if( ok )
          {
              // allways 0 but in db is a new record
              qDebug() << _model->query().lastInsertId().toInt();
          }
          else
          {
              qDebug() << _model->lastError().text();
          }
      }
      

      @

      My table looks like this:

      @
      CREATE TABLE mytable
      (
      id serial NOT NULL,
      field1 text,
      CONSTRAINT mytable_pkey PRIMARY KEY (id),
      )
      WITH (
      OIDS=TRUE
      );
      @

      How can I get a inserted ID? I need select new row in QTableView and I need this information.

      1 Reply Last reply Reply Quote 0
      • L
        lgeyer last edited by

        As far as I know lastInsertId() for PostgreSQL is supported at least with Qt 4.7. If this is not an option for you you might try the "RETURNING":http://www.postgresql.org/docs/8.3/interactive/sql-insert.html clause in your queries.

        1 Reply Last reply Reply Quote 0
        • H
          Hostel last edited by

          Thanks for reply. Tomorrow I will test Qt 4.7. lastInsertId() returns a OIDS number from Pg(I know from Qt doc), but only in QSqlQuery(I tested this a few minuts ago), but with QSqlTableModel return always 0. I known a RETURNING but I want to stay with QSqlTableModel. Maybe I will change a little driver to Pg to implement a insert with returning. I need think about it what would be better.

          1 Reply Last reply Reply Quote 0
          • First post
            Last post