-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostcrd.py
More file actions
49 lines (34 loc) · 1.4 KB
/
Copy pathpostcrd.py
File metadata and controls
49 lines (34 loc) · 1.4 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
45
46
47
48
49
import psycopg2
import json
class Postgres():
def __init__(self):
self.conn = psycopg2.connect("dbname=student user=postgres password=aashiq123")
self.c = self.conn.cursor()
def create_table(self):
self.c.execute("CREATE TABLE IF NOT EXISTS students(id SERIAL PRIMARY KEY, name TEXT, age INT, address TEXT)")
self.conn.commit()
def insert_content(self):
raw = json.load(open('/home/aashiq/Desktop/firstweek/postgresql/dat.json'))
for i in raw:
self.c.execute(f"INSERT INTO students(age, name, address) VALUES ({i['age']}, '{i['name']}', '{i['address']}')")
self.conn.commit()
def read_db(self):
self.c.execute("SELECT * FROM students")
return self.c.fetchall()
def delete_db(self, id):
self.c.execute(f"DELETE FROM students WHERE id={id}")
self.conn.commit()
def read_id(self, id):
dictformat = {}
self.c.execute(f"SELECT * FROM students WHERE id={id}")
for row in self.c.fetchall():
dictformat['id']=row[0]
dictformat['name']= row[1]
dictformat['age']=row[2]
dictformat['address']=row[3]
return dictformat
if __name__=='__main__':
database=Postgres()
# database.read_id(int(input()))
database.read_db()
# database.delete_db(2)