-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql_script2.py
More file actions
44 lines (37 loc) · 1.46 KB
/
sql_script2.py
File metadata and controls
44 lines (37 loc) · 1.46 KB
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
42
43
44
import psycopg2
def create_table():
conn = psycopg2.connect("dbname='database1' user='postgres' password='0000' host='localhost' port='5432'")
cur = conn.cursor()
cur.execute("CREATE TABLE IF NOT EXISTS store (item TEXT, quantity INTEGER, price REAL)")
conn.commit()
conn.close()
def insert(item, quantity, price):
conn = psycopg2.connect("dbname='database1' user='postgres' password='0000' host='localhost' port='5432'")
cur = conn.cursor()
cur.execute("INSERT INTO store VALUES (%s,%s,%s)", (item,quantity,price))
conn.commit()
conn.close()
def view():
conn = psycopg2.connect("dbname='database1' user='postgres' password='0000' host='localhost' port='5432'")
cur = conn.cursor()
cur.execute("SELECT * FROM store")
rows = cur.fetchall()
conn.close()
return rows
def delete(item):
conn = psycopg2.connect("dbname='database1' user='postgres' password='0000' host='localhost' port='5432'")
cur = conn.cursor()
cur.execute("DELETE FROM store WHERE item=%s", (item,))
conn.commit()
conn.close()
def update(quantity,price,item):
conn = psycopg2.connect("dbname='database1' user='postgres' password='0000' host='localhost' port='5432'")
cur = conn.cursor()
cur.execute("UPDATE store SET quantity=%s, price=%s WHERE item=%s", (quantity,price,item))
conn.commit()
conn.close()
# create_table()
update(11,16.5,"Orange")
# insert("Orange", 10, 15)
# delete("Orange")
print(view())