Using executemany in SQLite Efficiently Inserting Multiple Rows

PHOTO EMBED

Tue Jul 09 2024 08:05:25 GMT+0000 (Coordinated Universal Time)

Saved by @freepythoncode ##python #coding #python #programming #sqllite3 #database

import sqlite3

# Connect to the database
con = sqlite3.connect('database.db')

# Create a cursor object
cursor = con.cursor()

# Define the data to be inserted as a list of tuples
data = [
    ('John Doe', 'johndoe@example.com'),
    ('Jane Smith', 'janesmith@example.com'),
    ('Mike Johnson', 'mikejohnson@example.com')
]

# Use executemany() to insert the data into the "users" table
cursor.executemany("INSERT INTO users (name, email) VALUES (?, ?)", data)

# Commit the changes
con.commit()
content_copyCOPY