Python Forum
Insert into SQL Table only when Table is First Created?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Insert into SQL Table only when Table is First Created?
#1
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:
#----------------------------------------------------------------------------------------------
#                          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() 
#----------------------------------------------------------------------------------------------
Reply


Messages In This Thread
Insert into SQL Table only when Table is First Created? - by Extra - Jun-26-2022, 10:44 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  drawing a table with the status of tasks in each thread pyfoo 3 610 Mar-01-2024, 09:29 AM
Last Post: nerdyaks
  How to create a table with different sizes of columns in MS word pepe 8 2,081 Dec-08-2023, 07:31 PM
Last Post: Pedroski55
  Trying to get counts/sum/percentages from pandas similar to pivot table cubangt 6 1,691 Oct-06-2023, 04:32 PM
Last Post: cubangt
  dict table kucingkembar 4 889 Sep-30-2023, 03:53 PM
Last Post: deanhystad
  Going through HTML table with selenium emont 3 985 Sep-30-2023, 02:13 AM
Last Post: emont
Thumbs Up Convert word into pdf and copy table to outlook body in a prescribed format email2kmahe 1 893 Sep-22-2023, 02:33 PM
Last Post: carecavoador
  Using pyodbc&pandas to load a Table data to df tester_V 3 1,023 Sep-09-2023, 08:55 PM
Last Post: tester_V
  Find a string from a column of one table in another table visedwings049 8 1,413 Sep-07-2023, 03:22 PM
Last Post: deanhystad
Question Using SQLAlchemy, prevent SQLite3 table update by multiple program instances Calab 3 929 Aug-09-2023, 05:51 PM
Last Post: Calab
  Color a table cell based on specific text Creepy 11 2,560 Jul-27-2023, 02:48 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020