-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconnect_mysql.py
73 lines (62 loc) · 2.8 KB
/
connect_mysql.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/python
# Date: 2018-04-27
#
# Prerequisit: sudo apt-get install python2.7-mysqldb
#
# Description:
# Demo program to connect with MySQL, executing query and parsing query.
import MySQLdb
import datetime
def print_log(log_str):
print str(datetime.datetime.now()) + ' - ' + log_str
ctr = 0
print_log('Connecting MySQL...')
db = MySQLdb.connect('35.185.247.248', 'root', 'abc123', 'nickfury_dev')
cursor = db.cursor()
print_log('Executing query...')
cursor.execute("SELECT Users.Id, Users.PersonType, Users.Name, "
"Users.BuildingCode, Users.StartDate, Users.ReportingManager, "
"Users.Tier, Users.VendorCompany, Users.UserName, Users.Site, "
"Users.TrainingRole, Users.VendorTeamLead, "
"Users.VendorChannel, Users.VendorBillable, "
"Users.SupportedRegion "
"FROM Users WHERE Users.IsActive = 1")
print_log('Parsing result...')
for u in cursor.fetchall():
# print u[0], u[1], u[2], ...
ctr += 1
print_log('Total number of records: ' + str(ctr))
cursor.close()
db.close()
# Sample output: It takes less 2 seconds to fetch all 7689 records from
# database.
# hansrajd@hansrajd-linux:~/sample-programs$ python connect_mysql.py
# 2018-04-25 15:30:02.658969 - Connecting MySQL...
# 2018-04-25 15:30:03.586444 - Executing query...
# 2018-04-25 15:30:05.202192 - Parsing result...
# 2018-04-25 15:30:05.203024 - Total number of records: 7689
# hansrajd@hansrajd-linux:~/sample-programs$ python connect_mysql.py
# 2018-04-25 15:30:08.886860 - Connecting MySQL...
# 2018-04-25 15:30:09.821874 - Executing query...
# 2018-04-25 15:30:11.457682 - Parsing result...
# 2018-04-25 15:30:11.458619 - Total number of records: 7689
# hansrajd@hansrajd-linux:~/sample-programs$ python connect_mysql.py
# 2018-04-25 15:30:12.590674 - Connecting MySQL...
# 2018-04-25 15:30:13.536987 - Executing query...
# 2018-04-25 15:30:15.176142 - Parsing result...
# 2018-04-25 15:30:15.176704 - Total number of records: 7689
# hansrajd@hansrajd-linux:~/sample-programs$ python connect_mysql.py
# 2018-04-25 15:30:17.638755 - Connecting MySQL...
# 2018-04-25 15:30:18.580657 - Executing query...
# 2018-04-25 15:30:20.223432 - Parsing result...
# 2018-04-25 15:30:20.224486 - Total number of records: 7689
# hansrajd@hansrajd-linux:~/sample-programs$ python connect_mysql.py
# 2018-04-25 15:30:21.016207 - Connecting MySQL...
# 2018-04-25 15:30:21.937611 - Executing query...
# 2018-04-25 15:30:23.544153 - Parsing result...
# 2018-04-25 15:30:23.544598 - Total number of records: 7689
# hansrajd@hansrajd-linux:~/sample-programs$ python connect_mysql.py
# 2018-04-25 15:30:24.230144 - Connecting MySQL...
# 2018-04-25 15:30:25.169027 - Executing query...
# 2018-04-25 15:30:26.802720 - Parsing result...
# 2018-04-25 15:30:26.803314 - Total number of records: 7689