Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSqlQuery::execute after an insert returns always failure due to UNIQUE constraint validation. Double insertion while using QSqlQuery
QtWS25 Last Chance

QSqlQuery::execute after an insert returns always failure due to UNIQUE constraint validation. Double insertion while using QSqlQuery

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 780 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Ajesh Kumar T.T.
    wrote on last edited by
    #1

    Issue
    The following code block return failure due to UNIQUE constraint.

    QString query =
    QString("INSERT INTO UsedStID(StID,UsedID)VALUES(%1,%2)").arg(createdID).arg(stID);
    QSqlQuery queryExecuter(query);
    if(!queryExecuter.exec())
    {
    qDebug() << query <<"____" <<queryExecuter.lastError();
    }
    

    Solution
    Instead of following line :

    QSqlQuery queryExecuter(query);
    

    Use like:

    QSqlQuery queryExecuter;
    queryExecuter.exec(query);
    

    Reason
    The reason for this issue is, when you are passing a query string to the constructor it will execute this query and return resulting query object that contains data fetched from the DB (inserting result/error string/real data ifselect is used). Please see an example here http://doc.qt.io/qt-5/qsqlquery.html#details
    This code snippet:

    QSqlQuery query("SELECT country FROM artist");
    while (query.next()) {
    QString country = query.value(0).toString();
    doSomething(country);
    }
    

    will fetch the data from the artist table and creates a query object from which the one can retrieve data. It is small thing, but it is not mentioned in documentation.

    Note: - This is created as per the communication Dmitry who is a Qt support engineer.

    Ajesh Kumar T.T.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      More precisely you are executing the query twice hence the error since you have a unique constraint in your table.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      A 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi,

        More precisely you are executing the query twice hence the error since you have a unique constraint in your table.

        A Offline
        A Offline
        Ajesh Kumar T.T.
        wrote on last edited by
        #3

        @SGaist
        Yes. That is the conclusion.

        Ajesh Kumar T.T.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved