-
Notifications
You must be signed in to change notification settings - Fork 5
/
creating_table.py
36 lines (32 loc) · 1.22 KB
/
creating_table.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#03-07-2013
#creating a new table in an existing database
import sqlite3
def create_table(db_name,table_name,sql):
with sqlite3.connect(db_name) as db:
print(table_name)
cursor = db.cursor()
cursor.execute("select name from sqlite_master where name=?",(table_name,))
result = cursor.fetchall()
keep_table = True
if len(result) == 1:
response = input("The table {0} already exists, do you wish to recreate it? (y/n): ".format(table_name))
if response == "y":
keep_table = False
print("The {0} table will be recreated - all existing data will be lost".format(table_name))
cursor.execute("drop table if exists {0}".format(table_name))
db.commit()
else:
print("The existing animal table was kept")
else:
keep_table = False
if not keep_table:
cursor.execute(sql)
db.commit()
if __name__ == "__main__":
db_name = "coffee_shop.db"
sql = """create table Product
(ProductID integer,
Name text,
Price real,
primary key(ProductID))"""
create_table(db_name, "Product", sql)