In this video, you will learn how to insert records into MySQL table using Python step by step.
We’ll understand:
✔ Connecting Python with MySQL
✔ INSERT query
✔ Using placeholders
✔ Passing values safely
✔ commit method
✔ Practical example
This Python MySQL tutorial is specially designed for beginners.
🧠 What is Insert Record?
Insert record means:
👉 Adding new row into database table.
Here we insert data into user table inside pythondb.
✅ Complete Example (From Your Code)
import mysql.connector
conn = mysql.connector.connect(
host="localhost",
username="root",
password="",
db="pythondb"
)
cursor = conn.cursor()
query = "insert into user(id,name) values(%s,%s)"
values = (2,"Anbu")
cursor.execute(query, values)
conn.commit()
🧠 Step by Step Explanation
1️⃣ Import Connector
import mysql.connector
Loads MySQL module.
2️⃣ Connect Database
conn = mysql.connector.connect(
host="localhost",
username="root",
password="",
db="pythondb"
)
Connects Python with pythondb database.
3️⃣ Create Cursor
cursor = conn.cursor()
Cursor allows execution of SQL commands.
4️⃣ Write Insert Query
query = "insert into user(id,name) values(%s,%s)"
%s are placeholders.
They prevent SQL injection.
5️⃣ Pass Values
values = (2,"Anbu")
2 goes to id
Anbu goes to name
6️⃣ Execute Query
cursor.execute(query, values)
Runs INSERT command.
7️⃣ Commit Changes
conn.commit()
Saves data permanently into database.
Without commit, data will NOT be saved.
⭐ Result
One new row added:
id = 2
name = Anbu
🧠 Why Use %s Instead of Direct Values?
✔ Prevent SQL injection
✔ Safe parameter passing
✔ Cleaner code
⭐ Key Takeaways
✔ INSERT adds new record
✔ cursor executes SQL
✔ %s used as placeholder
✔ values tuple holds data
✔ commit saves record
⚠ Common Beginner Mistakes
❌ Forgetting commit
❌ Wrong table name
❌ Passing string without tuple
❌ MySQL server not running
📚 What You’ll Learn
Python MySQL insert
INSERT query
cursor execute
commit method
Database records
🔜 Upcoming Videos
Fetch Records
Update Records
Delete Records
👍 Like
💬 Comment doubts
🔔 Subscribe for Python tutorials
python mysql insert
python insert records mysql
mysql python insert query
python database insert
python mysql xampp
learn python step by step
#Python
#MySQL
#PythonMySQL
#InsertRecords
#Database
#XAMPP
#LearnPython
#PythonBasics
#PythonTutorial
#Programming