Interface 2
Interface 2
Interface 2
AIM:- To create table students in MySQL and insert values in it and display the content using Python.
ALGORITHM:
Step2: Import the mysql.connector” module which is used to connect MySQL database with in
Python.
Step3:- Create a variable and specify the ‘host’, ‘user’, ‘password’ and ‘database’.
Step 4: Specify another variable and create an instance of cursor by using ‘cursor()’.
Step 5: Now, create a table specify the table name “Students” and the required fields necessary and
execute.
Step 6: Specify a variable and insert a single value or multiple values using while loop in the table.
Step 7:- Execute the query using execute() function and followed by commit() function.
Step 8:- Fetch the records from the table in MySQL and print it .
PROGRAM:
import mysql.connector as ms
mycon=ms.connect(host="localhost",user="root",passwd="12345",
database="mysql")
if mycon.is_connected()==True:
print ("sucess")
cursor=mycon.cursor()
ans='y'
while ans=='y':
e=int(input("ENTER student NO."))
n=input("enter Name:")
m=float(input("enter the marks"))
g=input("enter grade")
p= input("project")
st="insert into students
values({},'{}',{},'{}','{}')".format(e,n,m,g,p)
cursor.execute(st)
mycon.commit()
ans=input("enter whether u want to continue(y/n)")
#fetching one data from mysql and printing
cursor.execute("select * from STUDENTS")
t=cursor.fetchall()
for i in t:
print(i)
mycon.commit()
mycon.close
AIM:- To create table Employee, search and print the records from MySQL using Python
ALGORITHM:
Step2: Import the mysql.connector” module which is used to connect MySQL database with in
Python.
Step3:- Create a variable and specify the ‘host’, ‘user’, ‘password’ and ‘database’.
Step 4: Specify another variable and create an instance of cursor by using ‘cursor()’.
Step 5: Now, create a table specify the table name “Emp” and the required fields necessary and
execute.
Step 7:- Execute the query using execute() function and followed by commit() function.
Step 8:- search for particular record or records according the query given , fetch and print it
PROGRAM:-
import mysql.connector as ms
mycon=ms.connect(host="localhost",user="root",passwd="12345",
database="neela")
cursor=mycon.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS EMP(eno int,ename
varchar(30), dept char(25), DOJ date, salary int)")
ans='y'
while ans=='y':
e=int(input("ENTER EMPLOYEE NO."))
n=input("enter Name:")
dp=input("enter the department")
doj=input("enter of DoJ")
s= int(input("enter the salary"))
query="insert into emp
values({},'{}','{}','{}',{})".format(e,n,dp,doj,s)
cursor.execute(query)
mycon.commit()
ans=input("enter whether u want to continue(y/n)")
#fetching data and printing
cursor.execute("select * from emp")
t=cursor.fetchall()
for i in t:
print(i)
OUTPUT
ENTER EMPLOYEE NO.1
enter Name:RITA
enter the departmentSALES
enter of DoJ12/09/16
enter the salary56000
enter whether u want to continue(y/n)y
SEARCH OF DATA WITH MORE THAN ONE CONSTRAINTS FROM THE TABLE
Enter department SALES
(1, 'RITA', 'SALES', datetime.date(2012, 9, 16), 56000)
(5, 'YAMUNA', 'SALES', datetime.date(2014, 7, 22), 78000)
OUTPUT :Records in the Emp and result of search
PROGRAM 23:
INTERFACE MYSQL WITH PYTHON –III-(UPDATE)
Q23. Create table Employee, insert and update the record in MySQL using python.
AIM:- To Create table Employee, insert and update the record in MySQL using Python.
ALGORITHM:
Step2: Import the mysql.connector” module which is used to connect MySQL database with in
Python.
Step3:- Create a variable and specify the ‘host’, ‘user’, ‘password’ and ‘database’.
Step 4: Specify another variable and create an instance of cursor by using ‘cursor()’.
Step 5: Now, create a table specify the table name “Emp” and the required fields necessary and
execute.
Step 7:- Execute the query using execute() function and followed by commit() function.
Step 9:- Fetch the records from the table in MySQL and print it .
PROGRAM:
import mysql.connector as ms
mycon=ms.connect(host="localhost",user="root",passwd="12345",d
atabase="neela")
cursor=mycon.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS EMP(eno int,ename
varchar(30), dept char(25), DOJ date, salary int)")
ans='y'
while ans=='y':
e=int(input("ENTER EMPLOYEE NO."))
n=input("enter Name:")
dp=input("enter the department")
doj=input("enter of DoJ")
s= int(input("enter the salary"))
query="insert into emp
values({},'{}','{}','{}',{})".format(e,n,dp,doj,s)
cursor.execute(query)
mycon.commit()
ans=input("enter whether u want to continue(y/n)")
#fetching data and printing
cursor.execute("select * from emp")
t=cursor.fetchall()
for i in t:
print(i)
OUTPUT
UPDATION OF DATA
Employee no. to be updated4
Enter new salary80000
(1, 'Devi', 'sales', datetime.date(2012, 12, 20), 20000)
(2, 'prajeet', 'hr', datetime.date(2012, 12, 20), 40000)
(3, 'Hari', 'Computer', datetime.date(2004, 1, 12), 60000)
(4, 'ferad', 'purchase', datetime.date(2006, 4, 13), 80000)
(5, 'john', 'sales', datetime.date(2015, 7, 25), 45000)
(6, 'Raju', 'Computer', datetime.date(2018, 12, 29), 55000)
(7, 'Nimal', 'Purchase', datetime.date(2020, 9, 15), 56050)
OUTPUT: DESC OF EMP TABLE
Step2: Import the mysql.connector” module which is used to connect MySQL database with in
Python.
Step3:- Create a variable and specify the ‘host’, ‘user’, ‘password’ and ‘database’.
Step 4: Specify another variable and create an instance of cursor by using ‘cursor()’.
Step 5: Now, create a table specify the table name “Product” and the required fields necessary and
execute.
Step 8:- Execute the query using execute() function and followed by commit() function.
Step 9:- Fetch the records from the table in MySQL and print it .
PROGRAM:
import mysql.connector as ms
mycon=ms.connect(host="localhost",user="root",passwd="12345",
database="mysql")
if mycon.is_connected()==True:
print ("Sucessfully connected")
cursor=mycon.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS PRODUCT (PNO INT
PRIMARY KEY,NAME CHAR(25),PRICE FLOAT)")
cursor.execute("DESC PRODUCT")
t=cursor.fetchall()
for i in t:
print(i)
ans='y'
while ans=='y':
pn=int(input("Enter Product No. :"))
n=input("Enter PName :")
p=float(input("Enter the price :"))
q=int(input("Enter no. of quality :"))
d= input("Date of Order :")
st="insert into product
values({},'{}',{},{},'{}')".format(pn,n,p,q,d)
cursor.execute(st)
mycon.commit()
ans=input("enter whether u want to continue(y/n)")
OUTPUT
successfully connected
('PNO', b'int', 'NO', 'PRI', None, '')
('NAME', b'char(25)', 'YES', '', None, '')
('PRICE', b'float', 'YES', '', None, '')
OUTPUT:
AIM:- To create table employee, , insert data and deleting the record in MySQL using Python
ALGORITHM:
Step2: Import the mysql.connector” module which is used to connect MySQL database with in
Python.
Step3:- Create a variable and specify the ‘host’, ‘user’, ‘password’ and ‘database’.
Step 4: Specify another variable and create an instance of cursor by using ‘cursor()’.
Step 5: Now, create a table specify the table name “Emp” and the required fields necessary and
execute.
Step 7:- Execute the query using execute() function and followed by commit() function.
Step 9:- Fetch the records from the table in MySQL and print it .
PROGRAM:-
import mysql.connector as ms
mycon=ms.connect(host="localhost",user="root",passwd="12345",
database="neela")
cursor=mycon.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS EMP(eno int,ename
varchar(30), dept char(25), DOJ date, salary int)")
ans='y'
while ans=='y':
e=int(input("ENTER EMPLOYEE NO."))
n=input("enter Name:")
dp=input("enter the department")
doj=input("enter of DoJ")
s= int(input("enter the salary"))
query="insert into emp
values({},'{}','{}','{}',{})".format(e,n,dp,doj,s)
cursor.execute(query)
mycon.commit()
ans=input("enter whether u want to continue(y/n)")
OUTPUT
OUTPUT: MySQL
TABLE BEFORE DELETION:
TABLE AFTER DELETION: