-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCustomerDao.py
More file actions
73 lines (63 loc) · 2.44 KB
/
CustomerDao.py
File metadata and controls
73 lines (63 loc) · 2.44 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
72
73
import mysql.connector
class CustomerDao:
def __init__(self):
self.mydb = mysql.connector.connect(
host="127.0.0.1",
user="root",
port=33061,
password="Test@2020",
database="employees"
);
self.mycursor = self.mydb.cursor();
def insertRecord(self, customer):
try:
mydb = self.mydb;
mycursor = self.mycursor;
sql = "INSERT INTO customers (name, address) VALUES (%s, %s)";
values = (customer.name, customer.address);
mycursor.execute(sql, values);
mydb.commit();
print(mycursor.rowcount, "Record Inserted");
except mysql.connector.Error as err:
print("Something went wrong: {}".format(err))
def insertRecords(self):
try:
mydb = self.mydb;
mycursor = self.mycursor;
sql = "INSERT INTO customers (name, address) VALUES (%s, %s)";
values = val = [
('Peter', 'Lowstreet 4'),
('Amy', 'Apple st 652'),
('Hannah', 'Mountain 21'),
('Michael', 'Valley 345'),
('Sandy', 'Ocean blvd 2'),
('Betty', 'Green Grass 1'),
('Richard', 'Sky st 331'),
('Susan', 'One way 98'),
('Vicky', 'Yellow Garden 2'),
('Ben', 'Park Lane 38'),
('William', 'Central st 954'),
('Chuck', 'Main Road 989'),
('Viola', 'Sideway 1633')
]
mycursor.executemany(sql, values);
mydb.commit();
print(mycursor.rowcount, "Records Inserted");
except mysql.connector.Error as err:
print("Something went wrong: {}".format(err))
def getCustomers(self):
mycursor = self.mycursor;
mycursor.execute("Select * from customers");
customers = mycursor.fetchall();
for customer in customers:
print(customer);
return customers;
def createDatabase(self):
self.mycursor.execute("Create database employees");
def createTable(self):
self.mycursor.execute("""create table if not exists employees.customers
(
name varchar(40) null,
address varchar(200) null
);
""");