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()

 

If you are Happy with DataFlair, do not forget to make us happy with your positive feedback on Google

courses

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

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