Last active
September 7, 2021 12:03
-
-
Save BexTuychiev/dab70ea045c877c8b6bc41b84acd926c to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
# Create a function to connect to a database with SQLite | |
def sqlite_connect(db_name): | |
"""Connect to a database if exists. Create an instance if otherwise. | |
Args: | |
db_name: The name of the database to connect | |
Returns: | |
an sqlite3.connection object | |
""" | |
try: | |
# Create a connection | |
conn = sqlite3.connect(db_name) | |
except sqlite3.Error: | |
print(f"Error connecting to the database '{db_name}'") | |
finally: | |
return conn |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The
conn
object isn't defined if there is an error.You should either remove the
finally
statement or put thereturn
statement in thetry
block.