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

Write Python code to increase age of all employees by 1 year in the Employee table of database HTMdb with userid HRMan and password HRMANexe@pwd.

 

Answer:

 

import mysql.connector

 

mydb = mysql.connector.connect('localhost','HRMan','HRMANexe@pwd','HRM')

cursor = mydb.cursor()

sql = "UPDATE Employee set Age=Age+1;"

try:

mydb.execute(sql)

mydb.commit()

except:

mydb.rollback()

cursor.close()

mydb.close()

 

Go Ad-free
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 and Computer Science at Teachoo