Adding a Record to Mysql Database - The Advantages of Learning Python Programming for Beginners: A Comprehensive Guide

Latest

Dive into the world of Python programming with our comprehensive resources

Music

logo

Monday, February 26, 2024

Adding a Record to Mysql Database

 

In this article we will add records to the MySql database. First, we connect to the dbemployee database. Then we add a record to the staff table with the insert into method. 


Output:

 


 

 








Figure 1


















Figure 2


import mysql.connector

 

con1 = mysql.connector.connect(

    host="localhost",

    user="root",

    password="1234",

    database="dbemployee"

)

cur = con1.cursor()

 

try:

 

 sql = "INSERT INTO staff (FirstName, LastName, Contact, Password) VALUES (%s, %s, %s, %s)"

 val = ("Carla", "Thompson","carlatson951@hotmail.com","9528")

 

 cur.execute(sql, val)

 

 con1.commit()

 

 print(cur.rowcount, "Record inserted successfully into staff table" +"\n"+ "staff tablosunun icerisine basarili bir sekilde kayit girisi yapildi.")

 

 cur.close()

 

except mysql.connector.Error as error:

    print("Failed to insert record into staff table {}".format(error)+"\n"+ "staff tablosuna kayit girerken hata olustu {}".format(error))

 

finally:

    if con1.is_connected():

        con1.close()

        print("MySQL connection is closed"+"\n"+ "MySQL baglantisi kapatildi.")

 

Hope to see you in the next article. Happy coding.

No comments:

Post a Comment

Featured post

Python Introduction

What is Python? Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. Its high-level built ...