-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_base_connector.py
53 lines (48 loc) · 2 KB
/
data_base_connector.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
42
43
44
45
46
47
48
49
50
51
52
53
# Imporitng Connectors for Connecting Python MySQL Database
import mysql.connector as connector
# Class Name
class DataBaseConnector:
# Constructor Method
def __init__(self):
self.mydb = connector.connect(
host="localhost", user="root", password="Sk@tc#*2", database="python_test")
query = 'CREATE TABLE IF NOT EXISTS customers(userid INT PRIMARY KEY, username VARCHAR(200), phone VARCHAR(12))'
myCursor = self.mydb.cursor()
myCursor.execute(query)
# print("\"user_table\" Created in \"python_test\" Database")
# Insert Method
def insert_data(self, userid, username, phone):
query = "INSERT INTO customers(userid, username, phone) VALUES({}, '{}', '{}')".format(
userid, username, phone)
print(query)
cur = self.mydb.cursor()
cur.execute(query)
self.mydb.commit() # Commit only on with connections
print("User Data Are Saved In Database.")
# Fetching All Data In Database
def fetch_data(self):
query = "SELECT * FROM customers"
fcur = self.mydb.cursor()
fcur.execute(query)
for row in fcur:
print("User ID:", row[0])
print("User Name:", row[1])
print("User Phone:", row[2])
print()
# Deleting Data from Database (delete method)
def delete_data(self, user_id):
query = "DELETE FROM customers WHERE userid={}".format(user_id)
# print(query)
dcur = self.mydb.cursor()
dcur.execute(query)
self.mydb.commit() # Data is Parmanently Deleted
print("User", user_id, "is deleted.")
# Update data from Database (update method)
def update_data(self, user_id, new_username, new_phone):
query = "UPDATE customers SET username='{}', phone='{}' WHERE userid={}".format(
new_username, new_phone, user_id)
# print(query)
ucur = self.mydb.cursor()
ucur.execute(query)
self.mydb.commit()
print("New Data is Update")