-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconnect_fetch.py
49 lines (40 loc) · 1.53 KB
/
connect_fetch.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
import mysql.connector as mysql
from mysql.connector import Error
import getpass
#define the connector function
def connect_fetch():
''' function to connect and fetch rows in a database '''
#create a variable for the connector object
conn = None
#connection parameters
host = input('Enter Host for database')
database = input('Enter database name')
user = input('Enter user for database')
password = getpass.getpass("Enter password")
try:
conn = mysql.connect(host=host, database=database, user=user, password=password)
print('Connecting to database server....!')
if conn.is_connected:
print('Connected to database server')
#Select Query
sql_select_query = "select * from human"
cursor = conn.cursor()
cursor.execute(sql_select_query)
records = cursor.fetchall()
print("Total number of rows in human is: ", cursor.rowcount)
#print select output
print("\nPrinting each Buyer record")
for row in records:
print("Human Id : ", row[0])
print("Name : ", row[1])
print("Color : ", row[2])
print("Gender : ", row[3])
print("Bloodgroup : ", row[4], '\n')
except Error as e:
print('Not connecting due to: ', e)
finally:
if conn is not None and conn.is_connected():
conn.close()
print('database shutdown')
#call the function we just created
connect_fetch()