Skip to content

Instantly share code, notes, and snippets.

@BexTuychiev
Last active September 7, 2021 12:03
Show Gist options
  • Save BexTuychiev/dab70ea045c877c8b6bc41b84acd926c to your computer and use it in GitHub Desktop.
Save BexTuychiev/dab70ea045c877c8b6bc41b84acd926c to your computer and use it in GitHub Desktop.
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
@ABohynDOE
Copy link

The conn object isn't defined if there is an error.
You should either remove the finally statement or put the return statement in the try block.

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