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
QtWS25 Last Chance

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.
  • M4RZB4NiM Offline
    M4RZB4NiM Offline
    M4RZB4Ni
    wrote on 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
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on 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

      M4RZB4NiM 1 Reply Last reply
      1
      • SGaistS SGaist

        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.

        M4RZB4NiM Offline
        M4RZB4NiM Offline
        M4RZB4Ni
        wrote on 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

        jsulmJ the_T 2 Replies Last reply
        0
        • M4RZB4NiM M4RZB4Ni

          @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

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on 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
          • M4RZB4NiM M4RZB4Ni

            @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

            the_T Offline
            the_T Offline
            the_
            wrote on 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
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by VRonin
              #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

              • Login

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