sqlite_connectopn_class

PHOTO EMBED

Thu Mar 04 2021 08:51:56 GMT+0000 (Coordinated Universal Time)

Saved by @Bartok #python

import sqlite3


class DatabaseConnection:
    def __init__(self,host):
        self.connection = None
        self.host = host

    def __enter__(self):
        self.connection = sqlite3.connect(self.host)
        return self.connection

    def __exit__(self, exc_type, exc_val, exc_tb): #parameters in case of exception
        if exc_tb or exc_val or exc_type: # if one of thos erros happen
            self.connection.close()
        else:
            self.connection.commit()
            self.connection.close()
content_copyCOPY