-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDB.py
More file actions
71 lines (62 loc) · 2.36 KB
/
DB.py
File metadata and controls
71 lines (62 loc) · 2.36 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import mysql.connector as connector
#CRUD OPERATIONS
'''
CREATE (INSERT)
READ (SELECT)
UPDATE
DELETE
'''
class Dbhelper:
def __init__(self):
self.con=connector.connect(host='localhost',port='3306',user='root',password='database@7.',database='pythontest')
#whenever the program executed created IF NOT EXISTS. So if the table exists it will show the db table.
query='CREATE TABLE if not exists user(userId int primary key,userName varchar(20),phone varchar(12))'
#creating cursor. This will execute queries.
cur=self.con.cursor()
cur.execute(query)
print('Table Created')
#insert
def insert_user(self,userid,username,phone):
query="INSERT INTO user VALUES({},'{}','{}')".format(userid,username,phone)
cur = self.con.cursor()
cur.execute(query)
#Commit means changing in database physically or really.
#if we dont use commit() then it will only show output in cmd but the real db did not get changed. Thats why we have to use commit
self.con.commit()
print('User Saved To Database')
#Fetch ALL
def fetch_all(self):
query='SELECT * FROM user'
cur=self.con.cursor()
cur.execute(query)
for row in cur:
print('User Id:',row[0])
print('User Name:',row[1])
print('Phone:',row[2])
print()
print()
#Fetch One
def fetch_one(self,id):
query='SELECT * FROM user WHERE userId={}'.format(id)
cur=self.con.cursor()
cur.execute(query)
for row in cur:
print('User Id:',row[0])
print('User Name:',row[1])
print('Phone:',row[2])
#Delete User
def delete_user(self,id):
query='DELETE FROM user WHERE userID={}'.format(id)
cur=self.con.cursor()
cur.execute(query)
#Commit means it will delete the record in database physically or really.
self.con.commit()
print('User Record Deleted.')
#Update
def update_user(self,userid,newname,newphone):
query="UPDATE user set userName='{}',phone='{}' WHERE userId={}".format(newname,newphone,userid)
cur=self.con.cursor()
cur.execute(query)
#Commit means it will Update the record in database physically or really.
self.con.commit()
print('User Record Updated.')