0

How to insert new record to QSqlTableModel with respect to default values of DB? UPD: I am trying to call pTableModel->insertRecord(0, pTableModel->record(0)); or pTableModel->insertRecord(0, pTableModel->record()); or pTableModel->insertRecord(QSqlDatabase::database().record("tableName")); or pTableModel->insertRow(0);

Result the same, all fields are NULL instead of default values of DB.

N West
  • 6,768
  • 25
  • 40
Funt
  • 399
  • 8
  • 23
  • This question is incredibly vague. [What have you tried?](http://whathaveyoutried.com) Is this a QT question or a database question? We can't read your mind. – N West Jun 27 '12 at 18:06

1 Answers1

0

The fact that you're inserting at row 0 may be an issue. When I've tried doing that, I don't get the record I expect when I later ask for the record at row 1 (which is where the inserted record should be after a call of insertRecord(0, ...)). Also, are you calling pTableModel->submit() or pTableModel->submitAll()? If not, your record never gets committed to the database.

This code works for me:

QSqlRecord  rec( pTableModel->record() );

// Insert at end of table
pTableModel->insertRecord( -1, rec );
pTableModel->submitAll(); 
// Or, pTableModel->submit(), depending on the model's edit strategy

// Grab the last row.  It should be the one just inserted.
QSqlRecord  writtenRec( pTableModel->record( pTableModel->rowCount() - 1 ) );
j0k
  • 22,600
  • 28
  • 79
  • 90
kerchen
  • 16
  • 1
  • Thanks. Actually, now I don't use this idea for some reasons. But my idea was to get default DB values for a record without submitting of new record to the DB. But it seems that this impossible without writing additional code :) – Funt Nov 16 '12 at 15:00