Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. how to reset auto increment id of column in data base table of sqlite browser ?
Qt 6.12 LTS is out! See what's new in the release blog

how to reset auto increment id of column in data base table of sqlite browser ?

Scheduled Pinned Locked Moved Solved Mobile and Embedded
13 Posts 4 Posters 5.2k 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.
  • Q Qt embedded developer

    after deleting data of data base table i need to reset my auto increment id to 0 but it does not come.

       QString sSqliteQuery;
        QSqlQuery qQuery;
    
        sSqliteQuery =  "DELETE FROM "+sDBTableName[DBTABLE_SURVEY];
    

    #ifdef QDEBUG_ENABLE
    pMainApp.ObjSettings.DebugStation(sSqliteQuery);
    #endif
    pMainApp.ObjDbOperations.ExecuteQueryFromString(sSqliteQuery, qQuery);
    sSqliteQuery = "UPDATE sqlite_sequence SET seq = 0 WHERE name = "+sDBTableName[DBTABLE_SURVEY];
    #ifdef QDEBUG_ENABLE
    pMainApp.ObjSettings.DebugStation(sSqliteQuery);
    #endif
    pMainApp.ObjDbOperations.ExecuteQueryFromString(sSqliteQuery, qQuery);

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

    @Qt-embedded-developer I don't see where you're deleting the table?

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

    Q 1 Reply Last reply
    0
    • jsulmJ jsulm

      @Qt-embedded-developer I don't see where you're deleting the table?

      Q Offline
      Q Offline
      Qt embedded developer
      wrote on last edited by
      #3

      @jsulm delete all data from table not a table

      jsulmJ KroMignonK 2 Replies Last reply
      0
      • Q Qt embedded developer

        @jsulm delete all data from table not a table

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

        @Qt-embedded-developer Try to delete the whole table and create it again. Simply deleting all data will not reset auto increment id.

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

        1 Reply Last reply
        1
        • Q Qt embedded developer

          @jsulm delete all data from table not a table

          KroMignonK Offline
          KroMignonK Offline
          KroMignon
          wrote on last edited by
          #5

          @Qt-embedded-developer said in how to reset auto increment id of column in data base table of sqlite browser ?:

          delete all data from table not a table

          If it is a SQLite DB, you have also to reset to Autoincrement ID from the sequence table:

          delete from your_table;    
          delete from sqlite_sequence where name='your_table';
          

          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

          Q 1 Reply Last reply
          3
          • KroMignonK KroMignon

            @Qt-embedded-developer said in how to reset auto increment id of column in data base table of sqlite browser ?:

            delete all data from table not a table

            If it is a SQLite DB, you have also to reset to Autoincrement ID from the sequence table:

            delete from your_table;    
            delete from sqlite_sequence where name='your_table';
            
            Q Offline
            Q Offline
            Qt embedded developer
            wrote on last edited by
            #6

            @KroMignon said in how to reset auto increment id of column in data base table of sqlite browser ?:

            delete from sqlite_sequence where name='your_table'

            this logic does not work

            jsulmJ J.HilkJ KroMignonK 3 Replies Last reply
            0
            • Q Qt embedded developer

              @KroMignon said in how to reset auto increment id of column in data base table of sqlite browser ?:

              delete from sqlite_sequence where name='your_table'

              this logic does not work

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

              @Qt-embedded-developer said in how to reset auto increment id of column in data base table of sqlite browser ?:

              this logic does not work

              Well, you should show code and tell what exactly "does not work"...
              Any errors?

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

              1 Reply Last reply
              1
              • Q Qt embedded developer

                @KroMignon said in how to reset auto increment id of column in data base table of sqlite browser ?:

                delete from sqlite_sequence where name='your_table'

                this logic does not work

                J.HilkJ Offline
                J.HilkJ Offline
                J.Hilk
                Moderators
                wrote on last edited by
                #8

                @Qt-embedded-developer I seriously hope you have replaced 'your_table' with the actual name of your table.


                Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                Q: What's that?
                A: It's blue light.
                Q: What does it do?
                A: It turns blue.

                Q 1 Reply Last reply
                0
                • J.HilkJ J.Hilk

                  @Qt-embedded-developer I seriously hope you have replaced 'your_table' with the actual name of your table.

                  Q Offline
                  Q Offline
                  Qt embedded developer
                  wrote on last edited by
                  #9

                  @J-Hilk yes i have replace that name with my table name

                  1 Reply Last reply
                  0
                  • Q Qt embedded developer

                    @KroMignon said in how to reset auto increment id of column in data base table of sqlite browser ?:

                    delete from sqlite_sequence where name='your_table'

                    this logic does not work

                    KroMignonK Offline
                    KroMignonK Offline
                    KroMignon
                    wrote on last edited by
                    #10

                    @Qt-embedded-developer said in how to reset auto increment id of column in data base table of sqlite browser ?:

                    this logic does not work

                    How did you test if it works or not?
                    SQLite is a stateless DB, autoincrements are stored in "private" Sqlite table sqlite_sequence. If you delete the entries in this table, it will restart from 0.

                    Extract from SQLite documentation:

                    SQLite keeps track of the largest ROWID using an internal table named "sqlite_sequence". The sqlite_sequence table is created and initialized automatically whenever a normal table that contains an AUTOINCREMENT column is created. The content of the sqlite_sequence table can be modified using ordinary UPDATE, INSERT, and DELETE statements. But making modifications to this table will likely perturb the AUTOINCREMENT key generation algorithm. Make sure you know what you are doing before you undertake such changes. The sqlite_sequence table does not track ROWID changes associated with UPDATE statement, only INSERT statements.

                    It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                    Q 1 Reply Last reply
                    3
                    • KroMignonK KroMignon

                      @Qt-embedded-developer said in how to reset auto increment id of column in data base table of sqlite browser ?:

                      this logic does not work

                      How did you test if it works or not?
                      SQLite is a stateless DB, autoincrements are stored in "private" Sqlite table sqlite_sequence. If you delete the entries in this table, it will restart from 0.

                      Extract from SQLite documentation:

                      SQLite keeps track of the largest ROWID using an internal table named "sqlite_sequence". The sqlite_sequence table is created and initialized automatically whenever a normal table that contains an AUTOINCREMENT column is created. The content of the sqlite_sequence table can be modified using ordinary UPDATE, INSERT, and DELETE statements. But making modifications to this table will likely perturb the AUTOINCREMENT key generation algorithm. Make sure you know what you are doing before you undertake such changes. The sqlite_sequence table does not track ROWID changes associated with UPDATE statement, only INSERT statements.

                      Q Offline
                      Q Offline
                      Qt embedded developer
                      wrote on last edited by
                      #11

                      @KroMignon

                      The sqlite_sequence table does not track ROWID changes associated with UPDATE statement, only INSERT statements.

                      thank you KroMignon

                      i used update query with delete from that's why auto increment id not get reset with delete from query

                         QString sSqliteQuery;
                          QSqlQuery qQuery;
                      
                          sSqliteQuery =  "DELETE FROM "+sDBTableName[DBTABLE_SURVEY];
                      

                      #ifdef QDEBUG_ENABLE
                      pMainApp.ObjSettings.DebugStation(sSqliteQuery);
                      #endif
                      pMainApp.ObjDbOperations.ExecuteQueryFromString(sSqliteQuery, qQuery);
                      sSqliteQuery = "UPDATE sqlite_sequence SET seq = 0 WHERE name = "+sDBTableName[DBTABLE_SURVEY];
                      #ifdef QDEBUG_ENABLE
                      pMainApp.ObjSettings.DebugStation(sSqliteQuery);
                      #endif
                      pMainApp.ObjDbOperations.ExecuteQueryFromString(sSqliteQuery, qQuery);

                      KroMignonK 1 Reply Last reply
                      0
                      • Q Qt embedded developer

                        @KroMignon

                        The sqlite_sequence table does not track ROWID changes associated with UPDATE statement, only INSERT statements.

                        thank you KroMignon

                        i used update query with delete from that's why auto increment id not get reset with delete from query

                           QString sSqliteQuery;
                            QSqlQuery qQuery;
                        
                            sSqliteQuery =  "DELETE FROM "+sDBTableName[DBTABLE_SURVEY];
                        

                        #ifdef QDEBUG_ENABLE
                        pMainApp.ObjSettings.DebugStation(sSqliteQuery);
                        #endif
                        pMainApp.ObjDbOperations.ExecuteQueryFromString(sSqliteQuery, qQuery);
                        sSqliteQuery = "UPDATE sqlite_sequence SET seq = 0 WHERE name = "+sDBTableName[DBTABLE_SURVEY];
                        #ifdef QDEBUG_ENABLE
                        pMainApp.ObjSettings.DebugStation(sSqliteQuery);
                        #endif
                        pMainApp.ObjDbOperations.ExecuteQueryFromString(sSqliteQuery, qQuery);

                        KroMignonK Offline
                        KroMignonK Offline
                        KroMignon
                        wrote on last edited by
                        #12

                        @Qt-embedded-developer said in how to reset auto increment id of column in data base table of sqlite browser ?:

                        i used update query with delete from that's why auto increment id not get reset with delete from query

                        I do not clearly understand if you still have a problem with AUTOINCREMENT reset?
                        Does it work now?

                        It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                        Q 1 Reply Last reply
                        1
                        • KroMignonK KroMignon

                          @Qt-embedded-developer said in how to reset auto increment id of column in data base table of sqlite browser ?:

                          i used update query with delete from that's why auto increment id not get reset with delete from query

                          I do not clearly understand if you still have a problem with AUTOINCREMENT reset?
                          Does it work now?

                          Q Offline
                          Q Offline
                          Qt embedded developer
                          wrote on last edited by
                          #13

                          @KroMignon Dear i have drop table and recreate it. it works for reset auto increment id

                          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
                          • Unsolved