Python can connect with the databases like MySQL, SQLite and many more, you can use select SQL command to fetch records from the database.
Create a database with name mydb, under it create a table, name it as student with 3 fields i.e. rollno, name, and age, than run the code mentioned below:
Example 1: Select or Show data from database table using python code.
import mysql.connector myconnect = mysql.connector.connect( host="localhost", user="root", password="", database="mydb" ) mycursor = myconnect.cursor() sql="select * from student" mycursor.execute(sql) myresult = mycursor.fetchall() for row in myresult: print(row)
cursor.fetchall() - fetches all the rows cursor.fetchmany(size) - returns the number of rows specified by size cursor.fetchone() - method returns a single record