Skip to content

Instantly share code, notes, and snippets.

@Magnus167
Created June 21, 2024 08:38
Show Gist options
  • Save Magnus167/2aa2ed1472618a74c8bfdae5c611365e to your computer and use it in GitHub Desktop.
Save Magnus167/2aa2ed1472618a74c8bfdae5c611365e to your computer and use it in GitHub Desktop.
import mysql.connector
# Establish a connection
connection = mysql.connector.connect(
host='your_host',
user='your_username',
password='your_password',
database='your_database'
)
# Create a buffered cursor
cursor = connection.cursor(buffered=True)
# Execute a query
cursor.execute("SELECT * FROM your_table")
# Fetch rows in batches
batch_size = 100 # Adjust the batch size as needed
rows = cursor.fetchmany(batch_size)
while rows:
for row in rows:
print(row)
rows = cursor.fetchmany(batch_size)
# Close the cursor and connection
cursor.close()
connection.close()
@Magnus167
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment