CBSE Class 12 Informatics Practices Question 56 of 70

Importing/Exporting Data between CSV Files/MySQL and Pandas — Question 11

Back to all questions
11
Question

Question 11

If query is a string storing an SQL statement. Write statements so that the data is fetched based on query from SQL database Mydata.

Answer

Let query store the following SQL statement:

query = "SELECT * FROM EMPLOYEE WHERE department = 'Human Resource'"
import pandas as pd
import mysql.connector as sqltor
mycon = sqltor.connect(host = "localhost",
                        user = "root", 
                        passwd = "MyPass", 
                        database = "Mydata")      
query = "SELECT * FROM EMPLOYEE WHERE department = 'Human Resource'"
mdf = pd.read_sql(query, mycon)
print(mdf)
mycon.close()