Jun-26-2022, 10:44 PM
Hello,
I'm trying to add default values to my SQLite table when it first gets created using and INSERT statement.
Right now, every time I run the program the default values keep getting inserted into the table. So now I have 10 rows of the same values.
Is there a way to program this so the values only get inserted when the table is first created (Table does not exist), and if the table does exist, don't insert those values because they're already there.
Thanks in advance
This is what I have:
I'm trying to add default values to my SQLite table when it first gets created using and INSERT statement.
Right now, every time I run the program the default values keep getting inserted into the table. So now I have 10 rows of the same values.
Is there a way to program this so the values only get inserted when the table is first created (Table does not exist), and if the table does exist, don't insert those values because they're already there.
Thanks in advance
This is what I have:
#---------------------------------------------------------------------------------------------- # Create Category Database #---------------------------------------------------------------------------------------------- def createCategoryDatabase(): #Create a database (users.db) connection = sqlite3.connect("categories.db") cursor = connection.cursor() table = """CREATE TABLE IF NOT EXISTS Categories (ID INTEGER PRIMARY KEY AUTOINCREMENT, Category TEXT NOT NULL, Low_Quantity_Value INT NOT NULL);""" #Execute the creation of the table cursor.execute(table) #print("The database has been created") #Commit the changes connection.commit() #Add a default category cursor.execute(''' insert into Categories (Category, Low_Quantity_Value) values ('N/A','0') ''') connection.commit() #Close the connection connection.close() #----------------------------------------------------------------------------------------------