You are on page 1of 3

18.

To create a table ‘student’ inside the


database ‘school’ using python script mode as
interface.

Code:
#To create a table in MySQL using Python Interface

import mysql.connector
mydb = mysql.connector.connect (host=”localhost®,\
user=”root”, \
passwd=””,\
database=”school”)
mycursor = mydb.cursor()
mycursor.execute (“CREATE TABLE student (Rollno int (3) Primary key, \ Name varchar(15),age integer, city
char(8))”)

Output:

38
19. To add new column ‘marks’ in the student
table.

Code:
#To modify table student (adding a new column) in
#MySQL using Python Interface

import mysql.connector
mydb = mysql.connector.connect (host=”localhost”, \
user=”root”, \
passwd=””,\
database=”school”)
mycursor = mydb.cursor()
mycursor.execute(“Alter table student add(marks int (3)}*)

Output:
>>>

RESTART: C:/Users/Vishal/AppData/Local/Programs/Python/Python37-32/prog mysql.py

>>>

39
20. To view modified structure of student table
using python script.

Code:
#To view the modified structure of table student in
#MySQL using Python Interface

import mysql.connector
mydb = mysql.connector.connect (host=”localhost”,\
user=”root”,\
passwd=””, \
database=”school”)
mycursor = mydb.cursor ()
mycursor.execute (“Desc student”)
for x in mycursor:
print (x)

Output:
>>>

RESTART: C:/Users/Vishal/AppData/Local /Programs/ Python/Python37-32/prog_mysqipyth3.py


(‘Rollno’, ‘int(3)’, ‘NO’, ‘PRI’, None, ' ’)
(‘Name’, ‘varchar(15)’, ‘YES’, ‘ ’, None, ‘ ’)
(‘age’, ‘int(11)’, ‘yes’, ‘ ’, None, ‘ ’)
(‘city’, ‘char(8)’, ‘YES’, ‘ ’, None, ‘ ')
(‘marks’, ‘int(3)’, ‘YES’, ‘ ’, None, ‘ ’)
>>>

40

You might also like