Professional Documents
Culture Documents
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:
>>>
>>>
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:
>>>
40