How to Call SQL Query from Python
Python's top supported database is MySQL. You can run SQL queries from Python. Here're best examples of how to connect to MYSQL and access MYSQL tables from Python.
import mysql.connector

conn=mysql.connector.connect(user='root', password='password', host='localhost', database='sakila')
mycursor=conn.cursor()
mycursor.execute("show tables") # you won't see any result. You need to give print.
print(mycursor.fetchall())
Read: How to Print String in Next Line Easily
Here are Steps
- Import MySQL connecter
- Give user id, password details
- Issue SQL query
Python Logic to import MySQL connector
Note: If the MySQL connecter not installed in python, you need to install it using the below command.
pip3 install mysql-connector-python --allow-external mysql-connector-python

Supply user id and Password
mycursor=conn.cursor()
Issue SQL Query
print(mycursor.fetchall())
mycursor.execute("select * from customer") # you won't see any result. You need to give print.
print(mycursor.fetchall())
This way you can issue SQL queries in Python.
print(mycursor.fetchall())
This way you can issue SQL queries in Python.
Comments
Post a Comment
Thanks for your message. We will get back you.