Results 1 to 5 of 5

Thread: Insert record into PostgreSQL table using default DB values

  1. #1
    Join Date
    Jan 2010
    Location
    Poland Warsaw
    Posts
    25
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Insert record into PostgreSQL table using default DB values

    Hello.

    I am trying to insert new row into table using QSqlTableModel's insertRows() and setData() methods. In my table I have ID field which is filled using Posgresql sequence.

    How can I insert new row and not to worry about ID value. When I insert row using sql INSERT statement I just ommit ID (database fills it for me).

    Thanks.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,677
    Thanks
    13
    Thanked 1,596 Times in 1,524 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Insert record into PostgreSQL table using default DB values

    Generally, if you don't set a value for a column in a newly inserted model row, and that column has a default value in the underlying database, then that column will take on the default value when the changes are committed to the database. This should work fine with PostgreSQL if your "id" column is defined something like this:
    Qt Code:
    1. ALTER TABLE mytable
    2. ALTER COLUMN myid
    3. SET DEFAULT NEXTVAL('mytable_myid_seq');
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2010
    Location
    Poland Warsaw
    Posts
    25
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Insert record into PostgreSQL table using default DB values

    Yes. I have already done it. But how should look C++ Qt code.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,677
    Thanks
    13
    Thanked 1,596 Times in 1,524 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Insert record into PostgreSQL table using default DB values

    Call QSqlTableModel::insertRows(), call setData() a number of times to set what you want in the new row (don't set the id column), move to another row or submitAll() the row (depends on edit strategy). This is all straight out of the manual, nothing tricky.

    If you are having problems, provide us with some idea of what you are actually doing, what you expect to see, and what you actually see.

  5. #5
    Join Date
    Jan 2010
    Location
    Poland Warsaw
    Posts
    25
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Insert record into PostgreSQL table using default DB values

    Call QSqlTableModel::insertRows(), call setData() a number of times to set what you want in the new row (don't set the id column), move to another row or submitAll() the row (depends on edit strategy). This is all straight out of the manual, nothing tricky.
    This method doesn't work.

    For now I use:
    Qt Code:
    1. void DBEngine::beforeInsertRec(QSqlRecord &record)
    2. {
    3. record.setGenerated(0, false);
    4. }
    To copy to clipboard, switch view to plain text mode 

    slot connected to beforeInsert(QSqlRecord &record) signal to set ID field to not be generated in sql query.

    I've found this solution in this forum.

    Thanks.

Similar Threads

  1. Replies: 3
    Last Post: 11th December 2010, 21:28
  2. Fast insert record into MySql
    By weixj2003ld in forum Qt Programming
    Replies: 3
    Last Post: 15th July 2010, 10:08
  3. Replies: 2
    Last Post: 13th April 2010, 17:50
  4. Import PostgreSQL Values to SQLITE
    By raphaelf in forum Newbie
    Replies: 2
    Last Post: 24th May 2007, 18:56
  5. Import PostgreSQL Values to QT
    By raphaelf in forum Newbie
    Replies: 8
    Last Post: 23rd May 2007, 10:09

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.