Python Display My SQL Table Rows Columns Training
Python - Display My. SQL Table Rows & Columns Training Division - LK Domain Registry
Introduction • This module guides you how to issue a SQL SELECT Query from Python application to retrieve My. SQL table data. • We are using My. SQL Connector Python to select data from My. SQL table. • To perform a SQL SELECT query from Python, you need to follow these simple steps 2
STEP 01 • Use previously installed My. SQL Connector Python import mysql. connector 3
STEP 02 • Establish My. SQL database Connection from Python mydb = mysql. connector. connect( host = 'localhost', database = 'student' user = 'root', password = '', ) 4
STEP 03 • Define and execute the SELECT query using cursor. execute() method mycursor = mydb. cursor() mycursor. execute("SELECT * FROM personal") 5
STEP 04 • Get result from cursor object using a cursor. fetchall() myresult = mycursor. fetchall() 6
STEP 05 • Print result using print() print(myresult) 7
Complete Python code to select data • Complete the Python code as follows 8
SELECT query output • Output of the above program is 9
SELECT query output • To get the output line by line, change last print(myresult) with a for loop: • Output: 10
SELECT query output • Further modify the print(x) statement to print ‘Index Number’ and ‘Name’ • Output: 11
SELECT query output • Slightly better version • Output: 12
Output with alphabetical order of names • Use ORDER BY statement to get the output in particular order 13
Output with alphabetical order of names • The output of the above code will be as follows 14
Thank You! training@nic. lk 15
- Slides: 15