Computer Science - Class 12
Chapter 15 - Interface of Python with SQL Database

Create following table using Python code.

Table Name = Customer

Database – xyzcorp

Userid – Adminxyz

Password – Axydm12

CUSTNUMB CUSTNAME ADDRESS BALANCE CREDLIM SLSNUMB
124 TINA ADAMS 481 Tilak Lain, CP Delhi 41800.75 50,000 3
256 R VENKAT 215 Mylapore Chennai 100000.75 80,000 6
567 BHUVNA BALAJI 808, Bala Nagar Hyderabad 57,000.75 50,000 6
622 PRATHAM JAIN 149, Plot 182, sec-9 Dwarka, Delhi 57,500.75 80,000 12

Answer:

 

import mysql.connector

 

db=mysql.connector.connect('localhost','Adminxyz','Axydm12','xyzcorp')

cursor=db.cursor()

cursor.execute("DROP TABLE IF EXISTS CUSTOMER")

sql="Create Table Customer(CUSTNUMB int,CUSTNAME varchar(30),ADDRESS varchar(100),BALANCE float,CREDLIM int,SLSRNUMB int"

cursor.execute(sql)

cursor.close()

records={(124,'TINA ADAMS','481 Tilak lane, CP, Delhi',41800.75,50000,3),

     (256,'R VENKAT','215 Mylapore, Chennai',100000.75,80000,6),

     (567,'BHUVNA BALAJI','808 BalaNagar, Hyderabad',57000.75,50000,6),

     (622,'PRATHAM JAIN','149 Plot 182, sec-9

Dwarka,Delhi',57500.75,80000,12)}

sql_insert="INSERT INTO

Customer(CUSTNUMB,CUSTNAME,ADDRESS,BALANCE,CREDLIM,SLSRNUMB) VALUES (%s,%s,%s,%s,%s,%s)"

try:

cursor.executemany(sql,records)

db.commit()

except:

db.rollback()

cursor.close()

db.close()

 

Ask a doubt
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh has done his B.Tech from Indian Institute of Technology, Kanpur. He has been teaching from the past 14 years. He provides courses for Maths, Science, Social Science, Physics, Chemistry, Computer Science at Teachoo.