Results 1 to 2 of 2

Thread: PostgreSQL + QSqlRelationalTableModel + editing problem

  1. #1
    Join Date
    May 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question PostgreSQL + QSqlRelationalTableModel + editing problem

    Hi guys,
    I have a problem trying to update a PostgreSQL record.

    What I want to develop is a framework that would help me to speed up making db applications but I'm, having some beginner troubles .

    It seems like Qt doesn't like when I try to update a QSqlRelationalTableModel which has a QSqlRelation applied to it.

    I searched all around on internet and the only help i could find was this link that i applied
    http://qtwiki.org/QSqlRelationalTabl...te_table_model but it doesn't seem to work.

    the code is:


    Qt Code:
    1. sqlModel->setTable("regions");
    2. sqlModel->setPrimaryKey("id");
    3. sqlModel->setRelation(1, QSqlRelation("nations", "id", "nation"));
    4. sqlModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
    5. sqlModel->select();
    To copy to clipboard, switch view to plain text mode 


    I use this model to show the table data in a gridview

    and then, clicking on a row a second form pops up with edits and combos mapped to the model.

    the record creating code is:


    Qt Code:
    1. QSqlRecord TableInformationProvider::getRecord(bool newRecord){
    2. if (newRecord){
    3. record = sqlModel->record();
    4. setDefaultRecordValues(record);
    5. emit recordPrepared(record);
    6. } else {
    7. record = sqlModel->record(currentModelIndex.row());
    8. }
    9. return record;
    10. }
    To copy to clipboard, switch view to plain text mode 


    and the update code is:


    Qt Code:
    1. bool TableInformationProvider::setRecord(bool newRecord){
    2. int row = currentModelIndex.row();
    3. bool result = false;
    4. if (newRecord){
    5. result = sqlModel->insertRecord(-1, record) && sqlModel->submitAll();
    6. } else {
    7. result = sqlModel->setRecord(row, record) && sqlModel->submitAll();
    8. }
    9. if (!result) {
    10. QMessageBox::critical(0, qApp->tr("Database error"),
    11. sqlModel->lastError().text());
    12. }
    13. return result;
    14. }
    To copy to clipboard, switch view to plain text mode 


    the error message is:
    "ERROR: invalid input syntax for integer: "Italy"
    LINE 1: EXECUTE: qpsqlpstmt_1 (2, 'Italy', 'Piedmont', 2, 2)
    QPSQL: Unable to create query"

    the tables are:


    Qt Code:
    1. CREATE TABLE nations(
    2. id serial NOT NULL,
    3. nation character varying NOT NULL DEFAULT 100,
    4. CONSTRAINT nationspk PRIMARY KEY (id))
    5.  
    6. CREATE TABLE regions(
    7. id serial NOT NULL,
    8. nationid integer NOT NULL,
    9. region character varying(100) NOT NULL,
    10. CONSTRAINT regionspk PRIMARY KEY (id),
    11. CONSTRAINT nations_regionsfk FOREIGN KEY (nationid)
    12. REFERENCES nations (id) MATCH SIMPLE ON UPDATE CASCADE ON DELETE RESTRICT)
    To copy to clipboard, switch view to plain text mode 


    I'm pretty new on c++ and Qt (just a couple of months) so, please, be gentle.

    cheers
    Mirko

  2. #2
    Join Date
    May 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: PostgreSQL + QSqlRelationalTableModel + editing problem

    I found myself a workaround, hope it will help others.

    1) the application opens a View and not a query. the view is defined as follows: "select id, nationid, region, nationid as nationid2 from regions"
    2) I set the relation on the 4thcolumn:
    Qt Code:
    1. getModel()->setRelation(3, QSqlRelation("nations", "id", "nation"));
    To copy to clipboard, switch view to plain text mode 
    3) On the view swap the 4th and 3rd columns:
    Qt Code:
    1. QHeaderView *headerView = view->horizontalHeader();
    2. headerView->swapSections(3, 2);
    To copy to clipboard, switch view to plain text mode 
    4) After I get the record for insert/update I delete the lookup columns:
    Qt Code:
    1. int index = record.indexOf("nation");
    2. if (index>=0){
    3. record.remove(index);
    4. }
    To copy to clipboard, switch view to plain text mode 

    It is an awful workaround but it works.

    bye
    Mirko

Similar Threads

  1. Problem in connecting to PostgreSQL
    By c_srikanth1984 in forum Qt Programming
    Replies: 3
    Last Post: 24th April 2009, 08:18
  2. Postgresql QSqlRelationalTableModel empty table
    By RolandHughes in forum Qt Programming
    Replies: 0
    Last Post: 12th November 2008, 18:18
  3. QThread and PostgreSQL - a problem
    By Lesiok in forum Qt Programming
    Replies: 12
    Last Post: 9th April 2008, 10:07
  4. Replies: 5
    Last Post: 24th May 2007, 12:11
  5. PostgreSQL and QT4 automate id problem!
    By nnidza in forum Qt Programming
    Replies: 9
    Last Post: 31st December 2006, 00:16

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
  •  
Qt is a trademark of The Qt Company.