Results 1 to 10 of 10

Thread: PostgreSQL and QT4 automate id problem!

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Oct 2006
    Posts
    13
    Thanks
    3
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default PostgreSQL and QT4 automate id problem!

    I am having problem with my database application. I am using a postgreSQL and QT4. I have created a connection in the main.cpp and it works fine.
    There are other forms too, but none of them is using an inline function to automaticaly generate the id number to some of the tables.

    Here is the structure of the table:

    CREATE TABLE "Blood"
    (
    "GroupID" int4 NOT NULL,
    "Group" varchar(3),
    CONSTRAINT "Blood_pkey" PRIMARY KEY ("GroupID")
    )
    WITHOUT OIDS;
    ALTER TABLE "Blood" OWNER TO postgres;

    Here are the functions I use to insert a new data in the table:


    Qt Code:
    1. inline int generateId(const QString &table)
    2. {
    3. QSqlQuery query;
    4. query.exec(" SELECT MAX(GroupID) FROM " + table);
    5. if (!query.isActive())
    6. cerr << "Error" << endl;
    7. int id = 0;
    8. if (query.next())
    9. id = query.value(0).toInt() + 1;
    10. return id;
    11.  
    12. }
    13.  
    14. void Krv::AddRec()
    15. {
    16. int row = model->rowCount();
    17. model->insertRow(row);
    18. QModelIndex index = model->index(row, Group);
    19. tableView->setCurrentIndex(index);
    20. tableView->edit(index);
    21. }
    22.  
    23. void Krv::beforeInsertRec(QSqlRecord &record)
    24. {
    25. record.setValue("GroupID", generateId(table));
    26. }
    To copy to clipboard, switch view to plain text mode 

    I am constantly getting an error message as the query is not active.
    I can delete from the same form, I can insert a first record, but the id wont iterate.
    It always got the id=0 value? Can someone help???
    Last edited by wysota; 25th December 2006 at 10:49. Reason: missing [code] tags

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.