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. Check if a row exists Update Record, otherwise insert In Database

Check if a row exists Update Record, otherwise insert In Database

Scheduled Pinned Locked Moved Unsolved General and Desktop
sqlserverupdatequery
6 Posts 5 Posters 8.1k 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.
  • M Offline
    M Offline
    M4RZB4Ni
    wrote on 10 Aug 2016, 17:55 last edited by
    #1

    Hello
    i want to add some records to a database But i want to when User
    Clicked on the Button Program in the Begning Check if a row exists Update The Record, otherwise insert in Table
    I searched in Google For This,But Only Found SQL STATMENTS FOR STORED PROCEDURES Not C++ Codes Or Notes
    Anybody can Help me Please?

    Thanks
    M4RZB4Ni

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 10 Aug 2016, 21:57 last edited by
      #2

      Hi,

      Which type of SQL database are you targeting ? If SQLite, then you can do it in one query directly.

      Otherwise (and since you don't want to use only SQL), you'll have to run a select query to check if what you search already exist and based on that, call either an insert or update query. All this using QSqlQuery.

      Hope it helps.

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

      M 1 Reply Last reply 11 Aug 2016, 07:56
      1
      • S SGaist
        10 Aug 2016, 21:57

        Hi,

        Which type of SQL database are you targeting ? If SQLite, then you can do it in one query directly.

        Otherwise (and since you don't want to use only SQL), you'll have to run a select query to check if what you search already exist and based on that, call either an insert or update query. All this using QSqlQuery.

        Hope it helps.

        M Offline
        M Offline
        M4RZB4Ni
        wrote on 11 Aug 2016, 07:56 last edited by
        #3

        @SGaist
        Thanks
        I use SqlServer 2014
        i know this method
        but i dont know how we Understand that Record exists ?
        Can you Write a sample Example?
        Thanks again

        Thanks
        M4RZB4Ni

        J T 2 Replies Last reply 11 Aug 2016, 08:12
        0
        • M M4RZB4Ni
          11 Aug 2016, 07:56

          @SGaist
          Thanks
          I use SqlServer 2014
          i know this method
          but i dont know how we Understand that Record exists ?
          Can you Write a sample Example?
          Thanks again

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 11 Aug 2016, 08:12 last edited by
          #4

          @M4RZB4Ni Use SQL count() function: http://www.w3schools.com/sql/sql_func_count.asp

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

          1 Reply Last reply
          2
          • M M4RZB4Ni
            11 Aug 2016, 07:56

            @SGaist
            Thanks
            I use SqlServer 2014
            i know this method
            but i dont know how we Understand that Record exists ?
            Can you Write a sample Example?
            Thanks again

            T Offline
            T Offline
            the_
            wrote on 11 Aug 2016, 08:14 last edited by
            #5

            @M4RZB4Ni

            I have never used MSSQL Server but a quick search on <whatever> tells me that a simple insert into on duplicate key update (as in mysql) or replace into (sqlite) is not supported. (both needs unique constraints for several keys in the table)

            Examples how to solve this with SQL statements directly can be found here:

            http://stackoverflow.com/a/12202
            http://stackoverflow.com/a/108420

            -- No support in PM --

            1 Reply Last reply
            2
            • V Offline
              V Offline
              VRonin
              wrote on 11 Aug 2016, 08:20 last edited by VRonin 8 Nov 2016, 09:41
              #6

              If you have SQL server why not just create a stored procedure in the server and call it in C++?! It's the best option under all circumstances.

              If you can't use that then use a select then use something like:

              bool alreadyExist = false;
              {
              QSqlQuery query;
              query.prepare("select * from MyTable where rowID=:rowID");
              query.bindValue(":rowID",rowID);
              if(!query.exec()){
              // select failed
              }
              alreadyExist = query.next();
              }
              if(alreadyExist){
              // update
              }
              else{
              //insert
              }
              

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              4

              3/6

              11 Aug 2016, 07:56

              • Login

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