Try this:
import sqlite3
conn = sqlite3.connect("NameOfYourDatabase.db")
cur = conn.cursor()
cur.execute("""CREATE TABLE IF NOT EXISTS inventory (
item_code text UNIQUE,
item_name text,
price INT,
quantity INT
)"""
try:
#INSERT whatever you want into the db here
except sqlite3.IntegrityError:
print("Item code already exists")
You can also make your item_code a PRIMARY KEY
as a PRIMARY KEY
is automatically set as UNIQUE
REMEMBER: You can have only one PRIMARY KEY
per table.
If your table is already created:
ALTER TABLE inventory
MODIFY item_code text NOT NULL UNIQUE;
With PRIMARY KEY
:
ALTER TABLE inventory
ADD PRIMARY KEY (item_code);
Learn more in this website:
https://www.w3schools.com/sql/sql_unique.asp