Python Program on Database Connection

Python course with 57 real-time projects - Learn Python

Program 1

import MySQLdb
from MySQLdb import *
try:
    con=MySQLdb.Connect(host="localhost",user="root",password="root",database="dataflair")
    print("Connection Success....")
    sql="insert into employee values(115,'hari','IT',8000)"
    cur=con.cursor()
    cur.execute(sql)
    con.commit()
    print("Record Inserted......",cur.rowcount)
except Exception as msg:
    print(msg)
finally:
    con.close()
    print("Connection close....")

 

Did you know we work 24x7 to provide you best tutorials
Please encourage us - write a review on Google

follow dataflair on YouTube

1 Response

  1. Mohammed Waseem says:

    HOW TO IMPORT MYSQLdb pls help sir

Leave a Reply

Your email address will not be published. Required fields are marked *