Closing the conn

This commit is contained in:
Lasse 2022-12-22 06:46:43 +00:00
parent baeecfef24
commit 109c889d59

View File

@ -1,14 +1,18 @@
import psycopg2 import psycopg2
import config
class DB: class DB:
def __init__(self, config): def __init__(self, config):
self.config = config
def con(self):
# Establish a connection to the database # Establish a connection to the database
self.database = psycopg2.connect( self.database = psycopg2.connect(
host=config['postgres_host'], host=self.config['postgres_host'],
database=config['postgres_database'], database=self.config['postgres_database'],
user=config['postgres_user'], user=self.config['postgres_user'],
password=config['postgres_password'] password=self.config['postgres_password']
) )
self.cursor = self.database.cursor() self.cursor = self.database.cursor()
@ -24,7 +28,7 @@ class DB:
self.database.commit() self.database.commit()
def insert_user(self, twitter_username, mastodon_username, source_tweet): def insert_user(self, twitter_username, mastodon_username, source_tweet):
self.con()
# Create the INSERT statement # Create the INSERT statement
sql = f"""INSERT INTO usernames (id, mastodon, source) sql = f"""INSERT INTO usernames (id, mastodon, source)
VALUES ('{twitter_username}', '{mastodon_username}', '{source_tweet}') VALUES ('{twitter_username}', '{mastodon_username}', '{source_tweet}')
@ -33,3 +37,4 @@ class DB:
self.cursor.execute(sql) self.cursor.execute(sql)
# Commit the transaction # Commit the transaction
self.database.commit() self.database.commit()
self.database.close()