I am using Sqlite to show who's address is school. This works and creates a table, however the next time the code is run it prints the table from last time as well so in this case where 3 people 'live' at school the first time it prints 3 people, the next time it is run it prints each name twice (total of 6) and so on. How would I change this code so that only one table is created and nothing is added to it each time I run the code. (In this case I want the result always to only have 3 lines).
import sqlite3
conn = sqlite3.connect('dbtest.db')
c = conn.cursor()
param = "School"
def create_table():
c.execute('CREATE TABLE IF NOT EXISTS myTestTable(name TEXT, age REAL, address TEXT)')
def data_entry():
c.execute("INSERT INTO myTestTable VALUES('James', 45, 'School')")
c.execute("INSERT INTO myTestTable VALUES('Jack', 15, 'School')")
c.execute("INSERT INTO myTestTable VALUES('Jeff', 14, 'Home')")
c.execute("INSERT INTO myTestTable VALUES('Bob', 14, 'School')")
conn.commit()
def query1():
c.execute("SELECT * FROM myTestTable WHERE address = ?",(param,))
while True:
row=c.fetchone()
if row == None:
break
print(row)
c.close()
conn.close()
create_table()
data_entry()
query1()
create_tableanddata_entryeach time.