Site icon DataFlair

Python Program to Connect with MySQL Database

Master Python with 70+ Hands-on Projects and Get Job-ready - Learn Python

Program 1

# Program for PDBC
import MySQLdb
try:
    con=MySQLdb.connect(host='localhost',user='root',password='root',database='DataFlair')   
    print("Data Base connected")
    eno=int(input("Enter Employee No:"))
    name=input("Enter Employee Name:")
    mobile=int(input("Enter Mobile No:"))
    age=int(input("Enter Employee age:"))

    sql="insert into employee values({},'{}',{},{})"
    sql=sql.format(eno,name,mobile,age)
    cur=con.cursor()
    cur.execute(sql)
    con.commit()
    print("Record inserted: ")
except Exception as obj:
    print(obj)    
finally:
    con.close()

 

Exit mobile version