Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can I use Drop and Create Table in a Button Click the same way you do in the Server compact tool? I am currently using code what have you tried below but its not resetting the ID number back to 1.

-- Script Date: 5/5/2021 4:47 PM  - ErikEJ.SqlCeScripting version 3.5.2.87
DROP TABLE [Import_Data];
CREATE TABLE [Import_Data] (
  [Id] INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL


What I have tried:

C#
 private void btn_Delete_Table_Info_Click(object sender, EventArgs e)
        {



            cmd = new SQLiteCommand();
            con.Open();
            cmd.Connection = con;
            cmd.CommandText = "delete from Import_Data where ID=  id ";
            cmd.ExecuteNonQuery();
            con.Close();

            MessageBox.Show("Successful Deleted", "Data Delete", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}
Posted
Updated 5-May-21 17:23pm
Comments
[no name] 5-May-21 22:20pm    
That's because in your "code", you're doing a "delete" (records) and not a drop (table).

1 solution

If I understand your question correctly, you would like to drop and recreate the table in order to reset the auto-incremented key for the table.

Before going to the details you should consider if you need to reset the auto-increment field at all. Auto-incremented keys are also called surrogate keys[^]. The point with this type of key is that the value is unique but meaningless to the application or to user. The sole purpose is to identify the record. So even if you have 1000 rows in the table, you delete them all, why does it matter if the id for the next record inserted is 1001? It shouldn't.

If there is a solid reason to reset the id: While you could drop and re-create the table it probably would be easier just to reset the auto-increment after you have deleted all the rows. Looking at the documentation
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.

So once yo have deleted all the rows you can set the increment value to zero using a statement like
SQL
UPDATE SQLITE_SEQUENCE 
SET    seq = 0
WHERE  name = 'Import_Data'

IMPORTANT! If you don't delete all of the rows, you don't want to set the value to something that would generate duplicates. In such case you would fetch the greatest value from you table and use that for the next value. Something like
SQL
UPDATE SQLITE_SEQUENCE 
SET    seq = (SELECT MAX(id)
              FROM Import_Data)
WHERE  name = 'Import_Data'
 
Share this answer
 
Comments
Member 12349103 6-May-21 6:35am    
Wendelius
I need to delete the table daily it will have over 10000 row, so after 100 days I would be in the million on Auto increment. How would I use the above statement in a Button Click?
Wendelius 6-May-21 12:47pm    
If you're worried that the value gets too high, I don't see this as a problem. The max value for an integer is 9'223'372'036'854'775'807 so if you have 10'000 rows per day it would take roughly 2'526'951'242'973 years before you hit the limit.

So as said I'd still recommend not resetting the value since it introduces different kinds of potential problems. But if you decide to do that, you would use ExecuteNonQuery just like you do with delete.
Member 12349103 6-May-21 15:13pm    
Wow did not know that
Thanks for the info

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900