Site icon DataFlair

How to Insert Data in Database Table Using Python Database Connection

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

In the ever-evolving landscape of Python application development, we redirect our attention to a pivotal task—inserting input records into a database table using Python. This skill is very important for developers who work with databases in their applications.

When developers learn how to easily add new data to their structured storage, it opens up many possibilities to improve their Python applications. This skill isn’t just about technical know-how; it’s like a door to creating applications that use data in flexible ways, changing as users and systems need them to.

Topic Explanation:

In this exploration, we embark on a journey to understand the process of inserting input records into a database table using Python. Initially, we’ll use Python Database Connection (PDBC) to connect our Python program to the database. Once connected, we’ll explore how to write SQL queries to insert input records into a particular table. We’ll navigate through the code, demonstrating step-by-step how to gather input data, construct an SQL query, and execute it to insert records into the database table.

This hands-on guide not only provides a practical understanding of database interaction in Python but also equips developers with the ability to enhance their applications with dynamic data.

Following the code like a map on an exciting journey, we uncover the step-by-step process of turning user input into real entries in the database table. Each line of code is like a magical spell, helping us collect what users input, create a clear message for the database, and execute it to add records to its well-organized structure.

This hands-on guide not only teaches us the technical side but also helps us truly understand how Python and databases work together. As developers armed with this new skill, we unlock doors to a world where our applications can adapt and change as more data comes in, keeping them lively and useful in the ever-changing world of software.

Prerequisite:

Understanding these prerequisites ensures a smoother learning experience as we explore the insertion of input records into a database table using Python.

Code With Comments:

# Importing MySQLdb library
import MySQLdb
from MySQLdb import *

try:
    # Establishing a connection to the MySQL database
    con = MySQLdb.Connect(host="localhost", user="root", password="root", database="dataflair")
    print("Connection Success")

    # User Input
    empid = int(input("Enter Employee ID:"))
    empname = input("Enter Employee Name:")
    empdept = input("Enter Employee Department:")
    empsal = int(input("Enter Employee Salary:"))

    # Query Execution
    sql = "insert into employee values('%d','%s','%s','%d')"
    value = (empid, empname, empdept, empsal)
    cur = con.cursor()
    
    # Executing the SQL query with user input values
    cur.execute(sql % value)

    # Committing the changes to the database
    con.commit()
    print("record inserted", cur.rowcount)

except Exception as obj:
    # Handling exceptions and printing the error message
    print(obj)

finally:
    # Closing the database connection
    con.close()
    print("Connection closed")

Output:

The program will print “record inserted” along with the number of rows affected, indicating a successful insertion if no exceptions occur.

Code Explanation:

Conclusion

In conclusion, navigating the Python realm of application development has led us through the essential task of seamlessly inserting input records into a database table. This skill is crucial for developers working with databases, as it allows them to easily add new data to their organized storage systems.

Mastering this skill isn’t just about technical needs—it’s also about opening doors to creating dynamic applications that can adapt to users’ and systems’ changing requirements. As we conclude this journey, it’s evident that this proficiency adds a layer of functionality and adaptability to Python applications, making them resilient in the face of evolving requirements.

Exit mobile version