Results 1 to 3 of 3

Thread: QSqlQuery: How to check for duplicates before inserting record

  1. #1
    Join Date
    Oct 2010
    Posts
    91
    Thanks
    38

    Default QSqlQuery: How to check for duplicates before inserting record

    Hello!

    I would like to check if a record already exists within an SQLite table before inserting it.

    While searching the forum I found this suggestion:

    http://www.qtcentre.org/threads/3471...-record-exist-
    in-qt-..

    Are there other possibilities or Qt-build-in functions or is it common to solve this using two queries?

    Kind regards,
    HomeR

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QSqlQuery: How to check for duplicates before inserting record

    If you use unique fields, then the check is done automatically by the database?

  3. The following user says thank you to tbscope for this useful post:

    homerun4711 (25th January 2011)

  4. #3
    Join Date
    Jan 2011
    Posts
    6
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery: How to check for duplicates before inserting record

    Hi,

    even when selecting for the record prior to inserting it, you can not be sure that it still does not exist when you do the actual insert operation (unless you lock the whole table in advance). Instead, check your data model, decide which columns make up the uniqueness of the record and then create a unique index on the table with these columns. Then, when inserting the record, the database returns an error indicating a violation of the unique index if the record already exists.

    HTH,

    Andreas

  5. The following user says thank you to littletux for this useful post:

    homerun4711 (25th January 2011)

Similar Threads

  1. Replies: 7
    Last Post: 27th November 2010, 15:55
  2. Replies: 4
    Last Post: 23rd October 2010, 17:54
  3. Replies: 1
    Last Post: 2nd October 2010, 22:11
  4. Replies: 3
    Last Post: 26th March 2010, 04:32
  5. Inserting Record
    By Nefastious in forum Newbie
    Replies: 3
    Last Post: 20th October 2009, 04:28

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.