# Buggy code
def __init__(self, config):
    path = config.core.db_filename
    config_dir, config_file = os.path.split(config.filename)
    config_name, _ = os.path.splitext(config_file)
    if path is None:
        path = os.path.join(config_dir, config_name + ".db")
    path = os.path.expanduser(path)
    if not os.path.isabs(path):
        path = os.path.normpath(os.path.join(config_dir, path))
    self.filename = path
    self._create()


# Traceback (OperationalError)
Traceback (most recent call last):
File "/home/TrollOP/willie/willie/bot.py", line 634, in call
exit_code = func(willie, trigger)
File "/home/TrollOP/willie/willie/modules/seen.py", line 50, in note
bot.db.set_nick_value(trigger.nick, 'seen_timestamp', time.time())
File "/home/TrollOP/willie/willie/db.py", line 150, in set_nick_value
nick_id = self.get_nick_id(nick)
File "/home/TrollOP/willie/willie/db.py", line 114, in get_nick_id
[slug]).fetchone()
File "/home/TrollOP/willie/willie/db.py", line 64, in execute
return cur.execute(*args, **kwargs)
OperationalError: database is locked

Traceback (most recent call last):
File "/home/TrollOP/willie/willie/bot.py", line 634, in call
exit_code = func(willie, trigger)
File "/home/TrollOP/willie/willie/modules/weather.py", line 129, in weather
woeid = bot.db.get_nick_value(trigger.nick, 'woeid')
File "/home/TrollOP/willie/willie/db.py", line 161, in get_nick_value
[nick.lower(), key]
File "/home/TrollOP/willie/willie/db.py", line 64, in execute
return cur.execute(*args, **kwargs)
OperationalError: database is locked

Traceback (most recent call last):
File "/home/TrollOP/willie/willie/bot.py", line 634, in call
exit_code = func(willie, trigger)
File "/home/TrollOP/willie/willie/modules/seen.py", line 50, in note
bot.db.set_nick_value(trigger.nick, 'seen_timestamp', time.time())
File "/home/TrollOP/willie/willie/db.py", line 152, in set_nick_value
[nick_id, key, value])
File "/home/TrollOP/willie/willie/db.py", line 64, in execute
return cur.execute(*args, **kwargs)
OperationalError: database is locked

# Fixed code
def __init__(self, config):
    # MySQL - mysql://username:password@localhost/db
    # SQLite - sqlite:////home/sopel/.sopel/default.db
    db_type = config.core.db_type

    # Handle SQLite explicitly as a default
    if db_type == "sqlite":
        path = config.core.db_filename
        config_dir, config_file = os.path.split(config.filename)
        config_name, _ = os.path.splitext(config_file)
        if path is None:
            path = os.path.join(config_dir, config_name + ".db")
        path = os.path.expanduser(path)
        if not os.path.isabs(path):
            path = os.path.normpath(os.path.join(config_dir, path))
        self.filename = path
        self.url = "sqlite:///%s" % path
    # Otherwise, handle all other database engines
    else:
        if db_type == "mysql":
            drivername = config.core.db_driver or "mysql"
        elif db_type == "postgres":
            drivername = config.core.db_driver or "postgresql"
        elif db_type == "oracle":
            drivername = config.core.db_driver or "oracle"
        elif db_type == "mssql":
            drivername = config.core.db_driver or "mssql+pymssql"
        elif db_type == "firebird":
            drivername = config.core.db_driver or "firebird+fdb"
        elif db_type == "sybase":
            drivername = config.core.db_driver or "sybase+pysybase"
        else:
            raise Exception("Unknown db_type")

        db_user = config.core.db_user
        db_pass = config.core.db_pass
        db_host = config.core.db_host
        db_port = config.core.db_port  # Optional
        db_name = config.core.db_name  # Optional, depending on DB

        # Ensure we have all our variables defined
        if db_user is None or db_pass is None or db_host is None:
            raise Exception(
                "Please make sure the following core "
                "configuration values are defined: "
                "db_user, db_pass, db_host"
            )
        self.url = URL(
            drivername=drivername,
            username=db_user,
            password=db_pass,
            host=db_host,
            port=db_port,
            database=db_name,
        )

    self.engine = create_engine(self.url)

    # Catch any errors connecting to database
    try:
        self.engine.connect()
    except OperationalError:
        print("OperationalError: Unable to connect to database.")
        raise

    # Create our tables
    BASE.metadata.create_all(self.engine)

    self.ssession = scoped_session(sessionmaker(bind=self.engine))