CBSE Class 12 Informatics Practices Question 68 of 70

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

Back to all questions
5
Question

Question 5

The DataFrame SDF stores the sales records of 100 salesmen. Write a program to store this as a table in database namely "company" on MySQL.

Solution
import pandas as pd
from sqlalchemy import create_engine
import pymysql

def sales(sdf):
    engine = create_engine('mysql+pymysql://root:Mypass@localhost/company')
    mycon = engine.connect()
    sdf.to_sql('Records', mycon, if_exists = 'replace', index = False)
Answer