forked from d-winch/python_course_section_14
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlite_example.py
41 lines (35 loc) · 1.09 KB
/
sqlite_example.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
40
41
import sqlite3
def create_table():
conn = sqlite3.connect("lite.db")
cursor = conn.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS store (item TEXT, quantity INTEGER, price REAL)")
conn.commit()
conn.close()
def insert(item, quantity, price):
conn = sqlite3.connect("lite.db")
cursor = conn.cursor()
cursor.execute("INSERT INTO store VALUES (?, ?, ?)", (item, quantity, price))
conn.commit()
conn.close()
def view():
conn = sqlite3.connect("lite.db")
cursor = conn.cursor()
cursor.execute("SELECT * FROM store")
rows = cursor.fetchall()
conn.close()
return rows
def delete(item):
conn = sqlite3.connect("lite.db")
cursor = conn.cursor()
cursor.execute("DELETE FROM store WHERE item=?", (item,))
conn.commit()
conn.close()
def update(item, quantity, price):
conn = sqlite3.connect("lite.db")
cursor = conn.cursor()
cursor.execute("UPDATE store SET quantity=?, price=? WHERE item=?", (quantity, price, item))
conn.commit()
conn.close()
update("Water Glass", 1, 1.99)
delete("Wine Glass")
print(view())