Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eder-projetos-dev/282d9f143caf43ec26e6a71e0e188b5b to your computer and use it in GitHub Desktop.
Save eder-projetos-dev/282d9f143caf43ec26e6a71e0e188b5b to your computer and use it in GitHub Desktop.
Python - Context Manager / Database Connection
import sqlite3
with sqlite3.connect("mydb.db") as conn:
cursor = conn.cursor()
cursor.execute("SELECT * FROM users")
results = cursor.fetchall()
@eder-projetos-dev
Copy link
Author

eder-projetos-dev commented Jun 3, 2023

Context Manager - Database Connection:

In the realm of data analysis, I often work with databases, and using a context manager for database connections is crucial. It ensures that the connection is properly closed, even if an exception occurs. I think it’s a clean and efficient way to manage database resources and maintain data integrity.

https://levelup.gitconnected.com/11-tricks-that-will-make-your-life-easier-as-a-python-developer-da29e4306675

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