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. MySql update
Forum Updated to NodeBB v4.3 + New Features

MySql update

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 4 Posters 539 Views 1 Watching
  • 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.
  • B Offline
    B Offline
    Buller
    wrote on last edited by
    #1

    I am having trouble updating a MySql table using QSqlDatabase and QSqlQuery. This works:
    query.prepare("UPDATE car SET cars_id='7' WHERE car_id='2'");
    But
    int last_cars_id = 7;
    query.prepare("UPDATE car SET cars_id='last_cars_id' WHERE car_id='2'");
    does not. So it must be related to
    cars_id='last_cars_id'
    I have also, among other things, tried
    query.prepare("UPDATE car SET cars_id=last_cars_id WHERE car_id='2'");
    but to no avail. It is probably a tiny fault, but I cannot see what it could be.

    jsulmJ B 2 Replies Last reply
    0
    • B Buller

      I am having trouble updating a MySql table using QSqlDatabase and QSqlQuery. This works:
      query.prepare("UPDATE car SET cars_id='7' WHERE car_id='2'");
      But
      int last_cars_id = 7;
      query.prepare("UPDATE car SET cars_id='last_cars_id' WHERE car_id='2'");
      does not. So it must be related to
      cars_id='last_cars_id'
      I have also, among other things, tried
      query.prepare("UPDATE car SET cars_id=last_cars_id WHERE car_id='2'");
      but to no avail. It is probably a tiny fault, but I cannot see what it could be.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Buller said in MySql update:

      query.prepare("UPDATE car SET cars_id='last_cars_id' WHERE car_id='2'");

      It does not work this way. Your int variable has nothing to do with your query string.
      Please take a look at https://doc.qt.io/qt-5/qsqlquery.html "Approaches to Binding Value" chapter.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • B Buller

        I am having trouble updating a MySql table using QSqlDatabase and QSqlQuery. This works:
        query.prepare("UPDATE car SET cars_id='7' WHERE car_id='2'");
        But
        int last_cars_id = 7;
        query.prepare("UPDATE car SET cars_id='last_cars_id' WHERE car_id='2'");
        does not. So it must be related to
        cars_id='last_cars_id'
        I have also, among other things, tried
        query.prepare("UPDATE car SET cars_id=last_cars_id WHERE car_id='2'");
        but to no avail. It is probably a tiny fault, but I cannot see what it could be.

        B Offline
        B Offline
        Bonnie
        wrote on last edited by Bonnie
        #3

        @Buller

        query.prepare("UPDATE car SET cars_id=" + QString::number(last_cars_id) + " WHERE car_id='2'");
        

        or

        query.prepare(QString("UPDATE car SET cars_id=%1 WHERE car_id='2'").arg(last_cars_id));
        
        1 Reply Last reply
        0
        • B Offline
          B Offline
          Buller
          wrote on last edited by
          #4

          Got it working. Thanks.

          mrjjM 1 Reply Last reply
          0
          • B Buller

            Got it working. Thanks.

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Buller
            please set it as solved then :)

            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