-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path06_Fetching_data.py
More file actions
35 lines (25 loc) · 866 Bytes
/
06_Fetching_data.py
File metadata and controls
35 lines (25 loc) · 866 Bytes
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
""" Fetching Data """
""" In order to select particular attribute columns from a table, we write the attribute names."""
# SELECT attr1, attr2 FROM table_name
""" In order to select all the attribute columns from a table, we use the asterisk ‘*’ symbol."""
# SELECT * FROM table_name
""" Example: Select data from MySQL table using Python"""
import mysql.connector
# Connect to the StudentDB database
dataBase = mysql.connector.connect(
host ="localhost",
user ="root",
passwd ="Akhil@0109",
database = "studentdb"
)
# Create a cursor object
cursorObject = dataBase.cursor()
# Execute the SQL query
sql_query = "SELECT student_id,name, branch FROM students"
cursorObject.execute(sql_query)
myresult = cursorObject.fetchall()
print("🎓 Student Records:")
for row in myresult:
print(row)
# disconnecting from server
dataBase.close()