-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_db.py
39 lines (32 loc) · 1.66 KB
/
create_db.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
36
37
38
39
# import the sqlite3 library to work with SQLite databases in Python
import sqlite3
# connect and open the database file database.db, the database will be created when it does not exists
conn = sqlite3.connect('database.db')
print("Opened database successfully")
# drop table if exists:
conn.execute('DROP TABLE IF EXISTS card;')
conn.execute('DROP TABLE IF EXISTS wish;')
# create a new table card with two columns:
# cardid - title
conn.execute('CREATE TABLE card (cardid INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, title TEXT)')
print("Table card created successfully")
# insert four rows in the newly created table
conn.execute("INSERT INTO card (title) VALUES ('Greetings')")
conn.execute("INSERT INTO card (title) VALUES ('Happy valentine''s day')")
conn.execute("INSERT INTO card (title) VALUES ('Happy Birthday')")
conn.execute("INSERT INTO card (title) VALUES ('Happy Birthday')")
conn.execute("INSERT INTO card (title) VALUES ('Happy Birthday')")
conn.execute("INSERT INTO card (title) VALUES ('I love you')")
conn.execute("INSERT INTO card (title) VALUES ('Happy Birthday')")
conn.execute("INSERT INTO card (title) VALUES ('Merry Christmas')")
conn.execute("INSERT INTO card (title) VALUES ('Congratulations')")
print("Cards inserted successfully")
# create a second table wish:
# wishid - uid - sender - message - cardid
conn.execute('CREATE TABLE wish (wishid INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,uid TEXT, sender TEXT, message TEXT, cardid INTEGER)')
# there are no wishes already sent, so the table stays empty
print("Table wish created successfully")
# commit all changes to the database, otherwise they will be lost
conn.commit()
# close the connection
conn.close()