Python

P
PythonHub
Progress 0%

Python

  • Home
  • History of Python
  • Applications of Python
  • Introduction To Python
    • What is Python
    • Character Set
    • Tokens in Python
    • Python Execution Mode
    • Variable And Identifiers
    • Data Types in Python
    • Operators And Expressions
    • Constants in Python
    • Assignment Statement
    • Input / Output in Python
    • Simple Python Scripts
    • Namespace in Python
    • šŸ“ Assignments
  • Operators in Python
    • Arithmetic Operators
    • Assignment Operators
    • Shorthand Assignment Operators
    • Relational Operators
    • Logical Operators
    • Bitwise Operators
    • Special Operators
    • šŸ“ Assignments
  • Input Output in Python
    • Accept Input
    • Output Formatting
    • šŸ“ Assignments
  • Conditional Statement
    • Decision Making
    • if Statement
    • IF-ELSE STATEMENT
    • IF-ELSE LADDER
    • NESTED IF-ELSE
    • Short Hand IF-ELSE
    • šŸ“ Assignments
  • Loops
    • Introduction to Loops
    • While Loop
    • Nested While Loop
    • šŸ“ While Loop Assignments
    • For Loop
    • For Loop Examples
    • Nested For Loop
    • Nested For Loop Examples
    • Infinite While Loops
    • Infinite For Loops
    • Break, Continue and Else in Loops
    • Difference Between For and While Loop
    • For Each Loop
    • šŸ“ For Each Assignments
    • šŸ“ All Loops Assignments
  • List
    • List in Python
    • Access List Elements
    • List Functions
    • Iterate (Loop) List
    • List Comprehension
    • šŸ“ Assignments
  • Tuple
    • Tuple in Python
    • Access Tuple Elements
    • Tuple Functions
    • Iterate (Loop) Tuple
    • Unpack Tuple
    • Tuple Comprehension
    • šŸ“ Assignments
  • Set
    • Set in Python
    • Access Set Elements
    • Set Methods
    • Iterate (Loop) Set
    • Pack/Unpack Set
    • Set Comprehension
    • šŸ“ Assignments
  • Dictionary
    • Dictionary
    • Access Dictionary Items
    • Dictionary Methods
    • Iterate (Loop) Dictionary
    • Formatting Dictionaries
    • Nested Dictionaries
    • Dictionary Comprehension
    • šŸ“ Assignments
  • Diff List Tuple Set Dictionary
    • List vs Tuple
    • List vs Set
    • List vs Dictionary
    • Tuple vs Set
    • Tuple vs Dictionary
    • Dictionary vs Set
    • šŸ“ Assignments
  • Exception
    • Error vs Exception
    • Types of Exception
    • Exception Handling
    • User Defined Exception
    • Logging Exception
    • šŸ“ Assignments
  • Functions
    • Introduction to Functions
    • Modular Programming
    • Types of Functions
    • Inbuilt Functions
    • Need For User-Defined Function
    • Elements of User Defined Function
    • Function Arguments
    • Nesting of Functions
    • Recursion
    • Global Local and Non Local
    • Python Lambda Functions
    • šŸ“ Assignments
  • Python Module
    • Introduction to Module
    • Inbuilt Modules in Python
    • User Defined Module
    • šŸ“ Assignments
  • File Handling
    • Introduction to Files
    • Create File
    • Read Files
    • Write to File
    • Rename File
    • Copy File
    • Move File
    • List Files in Directory
    • Binary Files
    • Zipping and Unzipping Files
    • šŸ“ Assignments
  • Strings
    • Basics of Strings
    • String Special Operators
    • String Formatting Operators
    • String Methods
    • šŸ“ Assignments
  • Regular Expressions
  • Python OOPS
    • Basics of Object Oriented
    • What are Classes and Objects?
    • Creating Class and Object
    • OOP vs Procedural Programming
    • Difference Between Classes and Objects
    • Constructors
    • Destructor
    • Built Class Methods and Attributes
    • Class and Instance Variables
    • Inheritance in Python
    • Single Inheritance
    • Multiple Inheritance
    • Multilevel Inheritance
    • Hierarchical Inheritance
    • Hybrid Inheritance
    • Abstraction
    • Method Overriding
    • Abstract Method
    • Interfaces in Python
    • Abstract Class vs Interface
    • Public, Private and Protected
    • Overloading vs Overriding
    • Inheritance vs Composition
    • Encapsulation
    • Polymorphism
    • Inner Classes
    • šŸ“ Assignments
  • Advanced Python
    • Iterator in Python
    • Generator in Python
    • Decorator in Python
    • Type Hints in Python
    • Async/Await Programming
    • Dataclasses in Python
    • Context Managers in Python
    • Property Decorator in Python
    • __slots__ in Python
    • Enums in Python
    • Walrus Operator in Python
    • Match-Case in Python
    • šŸ“ Assignments
  • Python Standard Library
    • Collections Module
    • Itertools Module
    • Functools Module
    • Datetime Module
    • JSON Module
    • OS Module
    • Sys Module
    • Random Module
    • Math Module
    • šŸ“ Assignments
  • Python Testing
    • Unit Testing in Python
    • Pytest Framework
    • Mocking in Python
    • šŸ“ Assignments
  • Python Best Practices
    • PEP 8 Style Guide
    • Docstrings in Python
    • Logging in Python
    • Code Optimization Tips
    • Debugging Techniques
    • šŸ“ Assignments
  • MySQL Database in Python
    • Introduction to MySQL with Python
    • DBMS vs File System
    • Connecting to MySQL Database
    • Create Database in MySQL
    • Create Table in MySQL
    • Insert Data in MySQL
    • Insert Multiple Rows
    • Select Data from MySQL
    • WHERE Clause in MySQL
    • Update Data in MySQL
    • Delete Data from MySQL
    • Parameterized Queries
    • Transaction Management
    • Error Handling
    • Connection Pooling
    • MySQL Drivers Guide
    • Joins in MySQL
    • Aggregation Functions
    • Backup MySQL Database
    • Best Practices
    • šŸ“ Assignments
  • MySQL Database Operations
    • SELECT Statement
    • MySQL Operators
    • DDL Statements
    • DML Statements
    • Subqueries
    • JOIN Operations
    • Aggregation
    • Case Study
    • šŸ“ Assignments
  • Graphics in Python
  • Threads in Python
    • Introduction to Threads
    • Process vs Threads
    • Concurrent Programming & GIL
    • Uses of Threads
    • Creating Threads
    • Single Tasking
    • Multi Tasking
    • Thread Synchronization
    • šŸ“ Assignments
  • Interview Questions & Answers
  • Python Case Studies
  • Multiple Choice Questions
  • šŸ“ Practice Problems
Get in Touch
  • tech2dsm@gmail.com

© Sankalan Data Tech

Python Language Interactive Tutorial

Python: Insert Multiple Rows in MySQL

Python MySQL Insert Multiple Rows - Complete Guide

Learn how to insert multiple rows into MySQL tables efficiently from Python.

Created by Sankalan Data Tech Team Verified
Data Engineers, Analysts, Scientists & Trainers
Created by experienced Python developers, data engineers, and data scientists to make programming easy through practical examples, real-world experience, and clear explanations.
On this page:
  • Why Insert Multiple Rows?
  • Single vs Multiple
  • SQL Syntax
  • The executemany() Method
  • Batch Insertion
  • Performance Tips
  • Error Handling
  • Real-World Example
  • Best Practices
  • Try It Yourself
  • Quiz
  • FAQ
Share this tutorial:
Twitter LinkedIn Facebook WhatsApp Reddit Telegram Email Copy Link
What You'll Learn Here
  • Why insert multiple rows — efficiency and speed benefits
  • Single vs multiple — comparison of both approaches
  • SQL syntax — how to write multi-row INSERT statements
  • executemany() method — Python's tool for batch insertion
  • Batch insertion — handling large datasets in chunks
  • Error handling — dealing with failures in batch operations

Why Insert Multiple Rows?

In the previous tutorial, we learned how to insert a single row into a MySQL table. But what if you need to insert hundreds or thousands of rows at once?

šŸ’” Key concept: Inserting multiple rows in one query is much faster than inserting them one by one. It reduces network traffic and database overhead significantly.

Think of it like sending a package versus a truckload. If you need to send 100 packages to the same place, you wouldn't make 100 separate trips. You'd load them all onto one truck and make one trip. The database works the same way!

1

Real-World Scenarios for Multiple Row Inserts

# Common scenarios where you need multiple row inserts

# 1. Data Migration
# Moving data from CSV files or other databases into MySQL

# 2. Bulk Imports
# Importing product catalogs, customer lists, or inventory data

# 3. Analytics
# Inserting large datasets for analysis and reporting

# 4. Data Sync
# Syncing data between different systems or databases

# 5. Initial Data Population
# Setting up a new database with initial data

# Example: Adding multiple students at once
students_to_add = [
    ("Rahul", "Sharma", 22, "rahul@email.com"),
    ("Priya", "Patel", 25, "priya@email.com"),
    ("Amit", "Singh", 24, "amit@email.com"),
    ("Sneha", "Reddy", 23, "sneha@email.com"),
    ("Vikram", "Kumar", 26, "vikram@email.com")
]

Why it matters:

  • Speed — 5-10 times faster than single row inserts
  • Network efficiency — fewer round trips to the database
  • Resource usage — less CPU and memory overhead
  • Transaction management — easier to manage as a single unit

Quick Check: Why is inserting multiple rows at once faster? (Answer: It reduces network round trips and database overhead)

Single Row vs Multiple Rows

2

Understanding the Difference

# ============================================================
# SINGLE ROW INSERT (Slow method)
# ============================================================

# This does 5 separate trips to the database
cursor.execute("INSERT INTO students (name, age) VALUES ('Rahul', 22)")
cursor.execute("INSERT INTO students (name, age) VALUES ('Priya', 25)")
cursor.execute("INSERT INTO students (name, age) VALUES ('Amit', 24)")
cursor.execute("INSERT INTO students (name, age) VALUES ('Sneha', 23)")
cursor.execute("INSERT INTO students (name, age) VALUES ('Vikram', 26)")
connection.commit()

# ā±ļø Takes about 0.5-1 second for 5 rows
# ā±ļø Takes about 5-10 seconds for 1000 rows

# ============================================================
# MULTIPLE ROW INSERT (Fast method)
# ============================================================

# This makes ONE trip to the database
cursor.executemany(
    "INSERT INTO students (name, age) VALUES (%s, %s)",
    [("Rahul", 22), ("Priya", 25), ("Amit", 24), ("Sneha", 23), ("Vikram", 26)]
)
connection.commit()

# ā±ļø Takes about 0.05-0.1 seconds for 5 rows
# ā±ļø Takes about 0.5-1 second for 1000 rows

# The difference becomes huge with more rows!

Performance comparison:

  • Single row — 5 round trips to the database
  • Multiple rows — 1 round trip to the database
  • 5 rows — 10x faster with multiple rows
  • 1000 rows — 10x faster with multiple rows
  • 10,000 rows — even bigger difference!

Quick Check: How many round trips does multiple row insert make? (Answer: One round trip)

SQL Syntax for Multiple Rows

3

Understanding the Multi-Row INSERT Syntax

# ============================================================
# BASIC SYNTAX
# ============================================================

# Single row
INSERT INTO students (name, age) VALUES ('Rahul', 22);

# Multiple rows
INSERT INTO students (name, age) 
VALUES 
    ('Rahul', 22),
    ('Priya', 25),
    ('Amit', 24),
    ('Sneha', 23),
    ('Vikram', 26);

# ============================================================
# SYNTAX BREAKDOWN
# ============================================================

# Notice the structure:
# 1. INSERT INTO table_name (columns)
# 2. VALUES
# 3. Each row in parentheses: (value1, value2)
# 4. Rows separated by commas: ,
# 5. Last row has no comma after it

# ============================================================
# FULL EXAMPLE WITH ALL COLUMNS
# ============================================================

INSERT INTO students (first_name, last_name, age, email, joined_date)
VALUES 
    ('Rahul', 'Sharma', 22, 'rahul@email.com', '2024-01-15'),
    ('Priya', 'Patel', 25, 'priya@email.com', '2024-01-15'),
    ('Amit', 'Singh', 24, 'amit@email.com', '2024-01-16');

# You can insert any number of rows this way!

Key points about multi-row INSERT syntax:

  • Each row is inside parentheses ()
  • Rows are separated by commas ,
  • The last row doesn't have a comma after it
  • The semicolon ; ends the statement
  • MySQL can handle thousands of rows in one query

Quick Check: What separates multiple rows in a multi-row INSERT? (Answer: Commas)

The executemany() Method

4

Python's Tool for Batch Insertion

# ============================================================
# USING executemany() - The Right Way
# ============================================================

import mysql.connector

# Connect to MySQL
connection = mysql.connector.connect(
    host="localhost",
    user="root",
    password="secret",
    database="myapp_db"
)
cursor = connection.cursor()

# ============================================================
# EXAMPLE 1: Inserting Students
# ============================================================

query = "INSERT INTO students (first_name, last_name, age) VALUES (%s, %s, %s)"

# Data as a list of tuples
students_data = [
    ("Rahul", "Sharma", 22),
    ("Priya", "Patel", 25),
    ("Amit", "Singh", 24),
    ("Sneha", "Reddy", 23),
    ("Vikram", "Kumar", 26)
]

# Insert all students in one go
cursor.executemany(query, students_data)
connection.commit()

print(f"āœ… {cursor.rowcount} students inserted successfully!")

# ============================================================
# EXAMPLE 2: Inserting Products
# ============================================================

product_query = """
    INSERT INTO products (product_name, price, stock_quantity)
    VALUES (%s, %s, %s)
"""

products = [
    ("Laptop", 999.99, 10),
    ("Mouse", 29.99, 50),
    ("Keyboard", 49.99, 30),
    ("Monitor", 299.99, 15)
]

cursor.executemany(product_query, products)
connection.commit()

print(f"āœ… {cursor.rowcount} products inserted!")

# Close connections
cursor.close()
connection.close()

Important points about executemany():

  • Syntax: cursor.executemany(query, data_list)
  • Data format: List of tuples
  • Returns: Number of rows inserted
  • Efficiency: One round trip to the database
  • Security: Uses parameterized queries

Quick Check: What data format does executemany() expect? (Answer: A list of tuples)

Batch Insertion – Handling Large Datasets

5

Inserting Thousands of Rows Efficiently

# ============================================================
# BATCH INSERTION - For Large Datasets
# ============================================================

def batch_insert(cursor, connection, data, batch_size=500):
    """
    Insert data in batches to avoid memory issues.
    
    Args:
        cursor: MySQL cursor
        connection: MySQL connection
        data: List of tuples to insert
        batch_size: Number of rows per batch (default: 500)
    """
    query = """
        INSERT INTO students (first_name, last_name, age, email, joined_date)
        VALUES (%s, %s, %s, %s, %s)
    """
    
    total_rows = len(data)
    inserted = 0
    
    try:
        # Process in batches
        for i in range(0, total_rows, batch_size):
            batch = data[i:i + batch_size]
            cursor.executemany(query, batch)
            connection.commit()
            
            inserted += len(batch)
            print(f"āœ… Batch {i//batch_size + 1}: Inserted {len(batch)} rows")
        
        print(f"šŸŽ‰ Total: {inserted} rows inserted successfully!")
        return inserted
        
    except mysql.connector.Error as e:
        connection.rollback()
        print(f"āŒ Error at batch: {e}")
        return inserted

# ============================================================
# GENERATING SAMPLE DATA
# ============================================================

# Generate 10,000 sample students
sample_data = []
for i in range(10000):
    sample_data.append((
        f"Student_{i}",
        f"LastName_{i}",
        18 + (i % 10),
        f"student_{i}@email.com",
        "2024-01-01"
    ))

print(f"šŸ“Š Generated {len(sample_data)} students")

# Insert in batches of 1000
batch_insert(cursor, connection, sample_data, batch_size=1000)

# Clean up
cursor.close()
connection.close()

Why batch insertion is important:

  • Memory efficient — processes data in chunks
  • Prevents timeouts — avoids long-running queries
  • Progress tracking — shows insertion progress
  • Error recovery — if one batch fails, others succeed
  • Optimal performance — 500-1000 rows per batch is ideal

Quick Check: What is the recommended batch size for large datasets? (Answer: 500-1000 rows per batch)

Performance Tips

6

Optimizing Your Inserts

# ============================================================
# PERFORMANCE TIPS FOR MULTIPLE ROW INSERTS
# ============================================================

# 1. Use executemany() instead of a loop
# āŒ BAD - One by one
for student in students:
    cursor.execute(query, student)
    connection.commit()

# āœ… GOOD - All at once
cursor.executemany(query, students)
connection.commit()

# 2. Commit after each batch, not after each row
# āŒ BAD - Committing each row
for student in students:
    cursor.execute(query, student)
    connection.commit()

# āœ… GOOD - Commit after batch
cursor.executemany(query, batch)
connection.commit()

# 3. Disable autocommit for bulk inserts
# āœ… GOOD - Manual commit
connection.autocommit = False
cursor.executemany(query, data)
connection.commit()

# 4. Use appropriate batch size
# āœ… GOOD - 500-1000 rows per batch
for batch in chunks(data, batch_size=500):
    cursor.executemany(query, batch)
    connection.commit()

# 5. Remove indexes before bulk insert (for very large datasets)
# ALTER TABLE students DROP INDEX index_name;
# Then re-create after insertion

Key performance tips:

  • Use executemany() — always for multiple rows
  • Batch commit — commit after each batch, not each row
  • Disable autocommit — speeds up bulk inserts
  • Optimal batch size — 500-1000 rows
  • Remove indexes — for very large datasets

Quick Check: When should you disable autocommit? (Answer: For bulk inserts to improve performance)

Error Handling

7

Dealing with Batch Insertion Errors

# ============================================================
# ERROR HANDLING FOR BATCH INSERTS
# ============================================================

def insert_with_error_handling(cursor, connection, data):
    """Insert multiple rows with comprehensive error handling"""
    
    query = """
        INSERT INTO students (first_name, last_name, age, email)
        VALUES (%s, %s, %s, %s)
    """
    
    successful_rows = 0
    failed_rows = []
    
    for idx, row in enumerate(data):
        try:
            cursor.execute(query, row)
            successful_rows += 1
            
            # Commit every 100 rows
            if successful_rows % 100 == 0:
                connection.commit()
                
        except mysql.connector.IntegrityError as e:
            # Duplicate key error
            connection.rollback()
            print(f"āš ļø Row {idx+1} skipped: Duplicate entry - {e}")
            failed_rows.append({"row": row, "error": "Duplicate"})
            
        except mysql.connector.DataError as e:
            # Data type mismatch
            connection.rollback()
            print(f"āš ļø Row {idx+1} skipped: Data error - {e}")
            failed_rows.append({"row": row, "error": "Data error"})
            
        except mysql.connector.Error as e:
            # Other database errors
            connection.rollback()
            print(f"āŒ Row {idx+1} failed: {e}")
            failed_rows.append({"row": row, "error": str(e)})
    
    # Final commit for remaining rows
    connection.commit()
    
    print(f"āœ… Inserted: {successful_rows} rows")
    print(f"āŒ Failed: {len(failed_rows)} rows")
    
    return {
        "successful": successful_rows,
        "failed": len(failed_rows),
        "failed_details": failed_rows
    }

Common errors and how to handle them:

  • IntegrityError — duplicate entries, foreign key violations
  • DataError — wrong data type, invalid values
  • OperationalError — connection issues, timeouts
  • Use rollback() — to undo changes on error
  • Track failed rows — for retry or logging

Quick Check: What should you do when a batch insert fails? (Answer: Rollback and handle the error appropriately)

Real-World Example: CSV Import

8

Importing Data from CSV Files

# ============================================================
# COMPLETE CSV IMPORTER
# ============================================================

import csv
import mysql.connector
from mysql.connector import Error

class CSVImporter:
    """Import CSV data into MySQL database"""
    
    def __init__(self, db_config):
        self.db_config = db_config
        self.connection = None
        self.cursor = None
    
    def connect(self):
        """Establish database connection"""
        try:
            self.connection = mysql.connector.connect(**self.db_config)
            self.cursor = self.connection.cursor()
            print("āœ… Connected to MySQL")
            return True
        except Error as e:
            print(f"āŒ Connection failed: {e}")
            return False
    
    def import_csv(self, csv_file, table_name, batch_size=500):
        """
        Import CSV data into MySQL table.
        
        Args:
            csv_file: Path to CSV file
            table_name: Target MySQL table
            batch_size: Rows per batch (default: 500)
        """
        try:
            # Read CSV file
            data = []
            with open(csv_file, 'r', encoding='utf-8') as file:
                reader = csv.DictReader(file)
                headers = reader.fieldnames
                
                for row in reader:
                    # Convert row to tuple in the right order
                    data.append(tuple(row[col] for col in headers))
            
            print(f"šŸ“„ Read {len(data)} rows from CSV")
            
            # Build INSERT query
            placeholders = ", ".join(["%s"] * len(headers))
            query = f"INSERT INTO {table_name} ({', '.join(headers)}) VALUES ({placeholders})"
            
            # Insert in batches
            total_inserted = 0
            for i in range(0, len(data), batch_size):
                batch = data[i:i + batch_size]
                
                try:
                    self.cursor.executemany(query, batch)
                    self.connection.commit()
                    
                    total_inserted += len(batch)
                    print(f"šŸ“¦ Batch {i//batch_size + 1}: Inserted {len(batch)} rows")
                    
                except Error as e:
                    self.connection.rollback()
                    print(f"āŒ Batch {i//batch_size + 1} failed: {e}")
                    continue
            
            print(f"šŸŽ‰ Import complete! {total_inserted} rows inserted")
            return total_inserted
            
        except FileNotFoundError:
            print(f"āŒ CSV file not found: {csv_file}")
            return 0
        except Exception as e:
            print(f"āŒ Error: {e}")
            return 0
    
    def close(self):
        """Clean up connections"""
        if self.cursor:
            self.cursor.close()
        if self.connection:
            self.connection.close()

# ============================================================
# USAGE EXAMPLE
# ============================================================

if __name__ == "__main__":
    db_config = {
        "host": "localhost",
        "user": "root",
        "password": "secret",
        "database": "myapp_db"
    }
    
    importer = CSVImporter(db_config)
    if importer.connect():
        importer.import_csv("students.csv", "students")
        importer.close()

This real-world example shows:

  • Reading CSV files with Python's csv module
  • Building dynamic INSERT queries from CSV headers
  • Batch insertion for large CSV files
  • Proper error handling with rollback
  • Progress tracking for the user

Quick Check: What module is used to read CSV files? (Answer: The csv module)

Best Practices

9

Guidelines for Multiple Row Inserts

# ============================================================
# BEST PRACTICES SUMMARY
# ============================================================

print("=" * 60)
print("BEST PRACTICES FOR MULTIPLE ROW INSERTS")
print("=" * 60)

# 1. Always use executemany() for > 10 rows
print("\n1. ALWAYS USE EXECUTEMANY()")
print("   - Faster than execute() in a loop")
print("   - One round trip to the database")
print("   - 5-10x faster for bulk inserts")

# 2. Use batching for large datasets
print("\n2. USE BATCHING FOR LARGE DATASETS")
print("   - Insert in batches of 500-1000 rows")
print("   - Prevents memory issues")
print("   - Avoids timeout errors")

# 3. Commit after each batch
print("\n3. COMMIT AFTER EACH BATCH")
print("   - Not after each row")
print("   - Reduces disk I/O")
print("   - Makes inserts faster")

# 4. Use parameterized queries
print("\n4. USE PARAMETERIZED QUERIES")
print("   - Protects against SQL injection")
print("   - Uses %s placeholders")
print("   - Never use f-strings for SQL")

# 5. Handle errors gracefully
print("\n5. HANDLE ERRORS GRACEFULLY")
print("   - Use try/except blocks")
print("   - Rollback on errors")
print("   - Continue with next batch")

# 6. Monitor progress
print("\n6. MONITOR PROGRESS")
print("   - Show batch completion")
print("   - Track total rows inserted")
print("   - Log errors for review")

Summary of best practices:

  • Use executemany() — always for multiple rows
  • Batch insertion — 500-1000 rows per batch
  • Commit after batch — not after each row
  • Parameterized queries — security first
  • Error handling — rollback and continue
  • Progress tracking — keep users informed

Quick Check: What is the most important rule for multiple row inserts? (Answer: Always use executemany() for > 10 rows)

Try It Yourself

Experiment with inserting multiple rows in the editor below.

Loading Pyodide... 0%
Python Code Editor
========================================
INSERT MULTIPLE ROWS - PRACTICE
========================================

šŸ“ Inserting multiple students at once...
----------------------------------------
āœ… Inserted 5 students successfully!

šŸ“ Inserting 3 more students...
āœ… Inserted 3 students successfully!

šŸ”„ Trying to insert a student with duplicate email...
āš ļø Duplicate email: rahul@email.com (skipped)
āœ… Inserted 0 students successfully!

šŸ“‹ All Students:
-------------------------------------------------------
ID: 1 | Rahul Sharma | Age: 22 | rahul@email.com
ID: 2 | Priya Patel | Age: 25 | priya@email.com
ID: 3 | Amit Singh | Age: 24 | amit@email.com
ID: 4 | Sneha Reddy | Age: 23 | sneha@email.com
ID: 5 | Vikram Kumar | Age: 26 | vikram@email.com
ID: 6 | Anjali Nair | Age: 21 | anjali@email.com
ID: 7 | Ravi Desai | Age: 27 | ravi@email.com
ID: 8 | Meera Iyer | Age: 23 | meera@email.com
-------------------------------------------------------
Total: 8 students

āœ… Multiple row insertion is efficient and fast!
šŸ†

You've Got It!

You now understand how to insert multiple rows into MySQL from Python. You know how to use executemany(), batch insertion, and handle errors effectively.

Quick Quiz

Test what you've learned:

1. Which method is used to insert multiple rows in Python's MySQL connector?
2. How much faster is batch insertion compared to single row insertion?
3. What is the recommended batch size for large datasets?
4. What happens if one row fails in executemany()?
5. What should you do to optimize performance for bulk inserts?

Frequently Asked Questions

What is the difference between execute() and executemany()? ā–¼

execute() runs a single query with one set of values. executemany() runs the same query multiple times with different values. It's much faster for inserting many rows because it reduces network overhead.

What's the maximum number of rows I can insert at once? ā–¼

The limit is set by the max_allowed_packet setting in MySQL (default is 4MB-16MB). For practical purposes, insert in batches of 500-1000 rows to avoid memory and timeout issues.

Why should I use parameterized queries? ā–¼

Parameterized queries protect against SQL injection attacks. They separate the SQL code from the data, making it impossible for malicious data to change the query structure.

What happens if I insert a duplicate email? ā–¼

If you have a UNIQUE constraint on the email column, MySQL will raise an IntegrityError. You should catch this error, rollback the transaction, and handle it appropriately.

Can I use executemany() with ON DUPLICATE KEY UPDATE? ā–¼

Yes! You can use INSERT ... ON DUPLICATE KEY UPDATE with executemany(). This will insert new rows and update existing ones if a duplicate key is found.

How can I track progress during a large insert? ā–¼

You can track progress by counting rows as you insert them. Use cursor.rowcount after each batch, or maintain a counter variable to show percentage completion.

Where to Go From Here

Now that you know how to insert multiple rows efficiently, check out these related topics:

Insert Single Row

Learn the basics of inserting data into MySQL.

Learn More →

Select Data

Learn how to query data from your MySQL tables.

Learn More →

WHERE Clause

Learn how to filter data with the WHERE clause.

Learn More →
Interview Resources
  • Python Syntax & Variables Interview Questions
  • Top SQL Interview Questions & Answers
  • SQL Joins: Displaying Data from Multiple Tables FAQ
  • Python Lists and Dictionaries Interview Questions
  • Python OOP Interview Questions
  • SQL Set Operators Interview Questions
Previous: Insert Data in MySQL Next: Select Data from MySQL →