Created
June 3, 2023 16:54
-
-
Save eder-projetos-dev/282d9f143caf43ec26e6a71e0e188b5b to your computer and use it in GitHub Desktop.
Python - Context Manager / Database Connection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sqlite3 | |
with sqlite3.connect("mydb.db") as conn: | |
cursor = conn.cursor() | |
cursor.execute("SELECT * FROM users") | |
results = cursor.fetchall() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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