Results 1 to 6 of 6

Thread: Problem with QSqlQuery and duplicate entry

  1. #1
    Join Date
    Jul 2010
    Posts
    17
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Problem with QSqlQuery and duplicate entry

    Hi guys:

    I'm having a problem with QSqlQuery. I'm trying to insert a record to a database, like this:

    Qt Code:
    1. QSqlDriver *driver = QSqlDatabase::database().driver();
    2. Q_CHECK_PTR(driver);
    3.  
    4. QSqlRecord record = driver->record(tableName);
    5. /* Here I fill the record with values */
    6.  
    7. QString smt = driver->sqlStatement(QSqlDriver::InsertStatement, tableName, record, false);
    8.  
    9. QSqlQuery query(smt);
    10. if (!query.exec())
    11. {
    12. QMessageBox::critical(this, qApp->applicationName(), tr("Error inserting into database. %1").arg(query.lastError().text()));
    13. return;
    14. }
    To copy to clipboard, switch view to plain text mode 

    Ok, the problem is that I get always a duplicate entry error: "Duplicate entry '47552' for key 'id' QMYSQL: Unable the execute query", but I'm sure that value doesn't exist. In fact, the record is inserted when query.exec() is executed (but I get the error anyways). The number differs everytime in +1 ('cause I'm getting it with a max(id)+1 query).

    The primary key isn't autoincremental.

    Any ideas?

    Thanks!!

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problem with QSqlQuery and duplicate entry

    It's an interesting way to insert values into a table.
    Did you try to use bindings instead?
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Aug 2009
    Location
    Greece
    Posts
    69
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with QSqlQuery and duplicate entry

    Though your query is highly unconventional the problem arises because the query is executed twice.
    Once in the constructor and once with the exec().
    This is how to execute just once:
    Qt Code:
    1. QSqlDriver *driver = QSqlDatabase::database().driver();
    2. Q_CHECK_PTR(driver);
    3.  
    4. QSqlRecord record = driver->record(tableName);
    5. /* Here I fill the record with values */
    6.  
    7. QString smt = driver->sqlStatement(QSqlDriver::InsertStatement, tableName, record, false);
    8.  
    9. QSqlQuery query;
    10. if (!query.exec(smt))
    11. {
    12. QMessageBox::critical(this, qApp->applicationName(), tr("Error inserting into database. %1").arg(query.lastError().text()));
    13. return;
    14. }
    To copy to clipboard, switch view to plain text mode 
    or
    Qt Code:
    1. QSqlQuery query(smt);
    2. if (!query.isValid()) .......
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jul 2010
    Posts
    17
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with QSqlQuery and duplicate entry

    spirit: I want to make it this way to don't have to worry if I change the type of the database (it doesn't happen very often, but...)

    Rhayader: that's it, thanks! I used this method for long time ago and never have any problem until now, that's why I didn't realize about the constructor...

    Thank you guys

  5. #5
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problem with QSqlQuery and duplicate entry

    Quote Originally Posted by Pit View Post
    spirit: I want to make it this way to don't have to worry if I change the type of the database (it doesn't happen very often, but...)
    Don't get how it can impact when a database is changed?
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  6. #6
    Join Date
    Jul 2010
    Posts
    17
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with QSqlQuery and duplicate entry

    Quote Originally Posted by spirit View Post
    Don't get how it can impact when a database is changed?
    If database changes maybe the INSERT statement changes with it. Like this I don't have to worry about that, I just fill the record and get the insert statement asociated to that record.

    Well, is just a way, I'm not saying is the best

Similar Threads

  1. Replies: 0
    Last Post: 26th October 2011, 13:44
  2. Flash Player 10.1 QTWebkit Text Entry Problem Mac
    By joeyjojo in forum Qt Programming
    Replies: 2
    Last Post: 27th March 2011, 16:15
  3. Replies: 1
    Last Post: 16th June 2010, 21:19
  4. runtime error, the procedure entry point problem
    By billconan in forum Qt Programming
    Replies: 2
    Last Post: 27th July 2008, 10:44

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.