-
Notifications
You must be signed in to change notification settings - Fork 0
/
12_read_from_MYSQL.py
41 lines (35 loc) · 1.21 KB
/
12_read_from_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
import mysql.connector
from mysql.connector import Error
def connect_to_db(db_name):
try:
mydb = mysql.connector.connect(
host="localhost",
user="sa",
password="14001003Sofia_",
database=db_name,
auth_plugin='caching_sha2_password'
)
if mydb.is_connected():
#print("You are connected")
cursor = mydb.cursor()
return cursor, mydb
else:
# print("You are still not connected")
return None, None
except Error as e:
print(f"Error: {e}")
return None, None
mydb = 'employment'
cursor, mydb_connection = connect_to_db(mydb)
if cursor:
cursor.execute('SELECT COUNT(*) AS number FROM info')
count = cursor.fetchall()
cursor.execute('SELECT name,height,weight FROM info order by height desc,weight')
selection=cursor.fetchall()
for item in range(len(selection)):
print(selection[item][0],' ',selection[item][1],' ',selection[item][2])
cursor.close()
else:
print('You are not connected')
if mydb_connection:
mydb_connection.close()