Today I have spent lots of time learning abit slowly with Postgres and I have been creating a code that does some stuff with the database such as insert, select etc etc.
I have realized that most of my code is copy paste when it comes to Connect & Disconnet and I know some people do not like it also depends on what I am doing so before people gets mad at me, do not take this as a bad code but more that I would like to imrpove of course <3
What I have done so far is:
import psycopg2
import psycopg2.extras
from loguru import logger
DATABASE_CONNECTION = {
"host": "TEST",
"database": "TEST",
"user": "TEST",
"password": "TEST"
}
def register_datas(store, data):
"""
Register a data to database
:param store:
:param data:
:return:
"""
ps_connection = psycopg2.connect(**DATABASE_CONNECTION)
ps_cursor = ps_connection.cursor()
ps_connection.autocommit = True
sql_update_query = "INSERT INTO public.store_items (store, name) VALUES (%s, %s);"
try:
data_tuple = (store, data["name"])
ps_cursor.execute(sql_update_query, data_tuple)
has_registered = ps_cursor.rowcount
ps_cursor.close()
ps_connection.close()
return bool(has_registered)
except (Exception, psycopg2.DatabaseError) as error:
logger.exception("Error: %s" % error)
ps_connection.rollback()
ps_cursor.close()
ps_connection.close()
return False
def get_all_keywords(keywords):
"""
Get all keywords
:param positive_or_negative:
:return:
"""
ps_connection = psycopg2.connect(**DATABASE_CONNECTION)
ps_cursor = ps_connection.cursor(cursor_factory=psycopg2.extras.DictCursor)
sql_update_query = "SELECT keyword FROM public.keywords WHERE filter_type = %s;"
try:
data_tuple = (keywords,)
ps_cursor.execute(sql_update_query, data_tuple)
all_keywords = [keyword["keyword"] for keyword in ps_cursor]
ps_cursor.close()
ps_connection.close()
return all_keywords
except (Exception, psycopg2.DatabaseError) as error:
logger.exception("Error: %s" % error)
ps_connection.rollback()
ps_cursor.close()
ps_connection.close()
return []
def check_if_store_exists(store):
"""
Check if the store exists in database
:param store:
:return:
"""
ps_connection = psycopg2.connect(**DATABASE_CONNECTION)
ps_cursor = ps_connection.cursor()
sql_update_query = "SELECT store FROM public.store_config WHERE store = %s;"
try:
data_tuple = (store,)
ps_cursor.execute(sql_update_query, data_tuple)
exists = bool(ps_cursor.fetchone())
ps_cursor.close()
ps_connection.close()
return exists
except (Exception, psycopg2.DatabaseError) as error:
logger.exception("Error: %s" % error)
ps_connection.rollback()
ps_cursor.close()
ps_connection.close()
return []
and I do see that I have same code where I do:
ps_connection = psycopg2.connect(**DATABASE_CONNECTION)
ps_cursor = ps_connection.cursor()
...
...
...
...
ps_cursor.close()
ps_connection.close()
return data
and to shorter this code, I wonder if its possible to do a function where I call the function that connects -> lets me do the query/execution -> close the connection and then return the data I want to return?