0

I was trying to make a column in a sqlite3 database auto-increment, it works perfectly, but when I try to delete a column, the auto-increment becomes a little off, here are some images of db browser to explain you the same enter image description here

Here, i created 3 columns, each with 7 rows, right now it works perfectly (fyi, the snippet_number row is auto-incrementing)

enter image description here

ok, so the problem starts here, when i delete the first row, the auto-increment for the first column is 2, which is wrong, as it should be 1

My question is that is their any way to fix this?

My code

This one creates the table, (it works flawlessly)

def set_up_db():
    conn = sqlite3.connect('RobertSnippetStoreage.db')
    cursor = conn.cursor()
    cursor.execute("CREATE TABLE IF NOT EXISTS snippets (snippet_number INTEGER PRIMARY KEY, snippet TEXT, snippet_language TEXT,snippet_name TEXT, snippet_description TEXT, date_created REAL, times_used INTEGER)")
    conn.commit()
    return conn, cursor

This one inserts snippets

def insert_snippet(snippet, snippet_language ,snippet_name, snippet_description):
    conn, cursor = set_up_db()
    cursor.execute("INSERT INTO snippets (snippet_number, snippet, snippet_language, snippet_name, snippet_description, date_created, times_used) VALUES (?, ?, ?,?, ?, ?, ?)", (None,snippet, snippet_language, snippet_name, snippet_description, str(time.time()), 0))
    conn.commit()

this one deletes snippets (this is where my problem is)

def delete_snippet(snippet_number):
    conn, cursor = set_up_db()

    cursor.execute("DELETE FROM snippets WHERE snippet_number=?", (snippet_number, ))

    conn.commit()
AkIonSight
  • 365
  • 3
  • 13
  • 1
    I think, the second answer in [this post](https://stackoverflow.com/questions/1601697/sqlite-reset-primary-key-field) could answer your question. Apart from that, with `snippet_number` being your primary key, wouldn't that mess up all references to other tables? – Roland Deschain Jan 11 '21 at 10:43
  • 1
    It is not wrong. This is how an autoincrement primary key column should behave. – forpas Jan 11 '21 at 10:45
  • @everyone, i just wanted to assign a unique number to each field, i just wanted to ask if it is a good design decision to do so? (Not correct the AUTOINCREMENT) feild – AkIonSight Jan 11 '21 at 15:32

0 Answers0