Posts

Showing posts from April, 2023

Interfacing SQL with Python : Update Record

import mysql.connector as cn con=cn.connect(host='localhost',user='root',password='root',database='stud_db') if con.is_connected():     print("connected to the database....") cr=con.cursor() print("==============Program to Update record==============") rn=int(input("Enter roll no. to update:")) marks=int(input("Enter New Marks:")) cr.execute("update student set marks=%s where rollno=%s"%(marks,rn)) con.commit() cr.execute("select * from student") data=cr.fetchall() for rec in data:    print(rec)

Interfacing SQL with Python

  import mysql.connector as cn print("==============Program to display records on screen==============") con=cn.connect(host='localhost',user='root',password='root',database='emp_db') if con.is_connected():     print("connected to the database....") cr=con.cursor() #cr.execute("create table empdemo(e_id varchar(20),ename varchar(20));") cr.execute("select * from emp") data=cr.fetchall() for rec in data:    print(rec)

Read a text file line by line and display each word separated by a #.

Image
# Read a text file line by line and display each word separated by a #. demo.txt: OUTPUT:

MySQL Practical

Create an Emp & Dept table and insert data. Implement the following SQL commands on the Emp or Dept table: o ALTER table to add new attributes / modify data type / drop attribute o UPDATE table to modify data o ORDER By to display data in ascending / descending order o DELETE to remove tuple(s) o GROUP BY and find the min, max, sum, count and average Create a new database as 'emp_db': create database emp_db; Switch to the newly created database 'emp_db': use emp_db; Create 'Emp' & 'Dept' tables as follows: Emp table: create table emp( emp_id int primary key, emp_name varchar(30) , emp_sal decimal(10,2), dept_id varchar(10), foreign key  (dept_id) references dept(dept_id) ); Dept table: create table dept( dept_id varchar(10) primary key, dept_name varchar(30), dept_loc varchar(30) ); Optional :to view structure of above tables:  desc emp; or  desc dept; ALTER table to add new attributes / modify data type / drop attribute: If you need to modify ta...