'Auto Incrementing Primary Key Using Entry Widgets into Table

I'm using entries to insert data points into a table where the 'ID' is auto-incrementing. I'm encountering an issue I had when I was working on importing a table with the id being based on auto incrementing, but the solutions I got for that haven't worked with this so far.

import tkinter as tk
import sqlite3
c.execute("""CREATE TABLE IF NOT EXISTS tbl (
     id INTEGER PRIMARY KEY NOT NULL,
     data text
     )""")

def add_equipment():
     conn = sqlite3.connect('database.db')
     c = conn.cursor()
     c.execute("INSERT INTO tbl VALUES(:ID + null, :data)
          {"data":data_ent.get()
          })
     conn.commit()
     conn.close()

Doing this gives me an error of did not supply value for binding parameter id, removing the ':id + null' gives me an error of 1 column doens't have a supplied value. I used a for loop on the import version of this, but when I tried to do a loop as:

     for row in c.fetchall():
          c.execute('variable for the insert command & data', row)

it gives me no error, but doesn't insert the data into the table. I assume the for loop is wrong, but I'm not sure what it should be since this is meant to insert a single record at a time.



Solution 1:[1]

def add_equipment():
     conn = sqlite3.connect('database.db')
     c = conn.cursor()
     c.execute("INSERT INTO tbl VALUES(:ID + null, :data)
          {"id":'NULL',
           "data":data_ent.get()
          })
     conn.commit()
     conn.close()

This gives id a binding parameter and allows the null to auto increment as supposed to.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Matiiss