43 lines
1.1 KiB
Python
43 lines
1.1 KiB
Python
from pathlib import Path
|
|
import sys
|
|
|
|
import psycopg2
|
|
|
|
ROOT_DIR = Path(__file__).resolve().parents[1]
|
|
CONN_DIR = ROOT_DIR / "conn"
|
|
if str(CONN_DIR) not in sys.path:
|
|
sys.path.insert(0, str(CONN_DIR))
|
|
|
|
import config
|
|
|
|
|
|
|
|
|
|
|
|
def conn():
|
|
try:
|
|
connection = psycopg2.connect(**config.POSTGRES_CONFIG)
|
|
|
|
return connection
|
|
# cursor = connection.cursor()
|
|
|
|
# postgres_insert_query = """ INSERT INTO mobile (ID, MODEL, PRICE) VALUES (%s,%s,%s)"""
|
|
# record_to_insert = (5, 'One Plus 6', 950)
|
|
# cursor.execute(postgres_insert_query, record_to_insert)
|
|
|
|
# connection.commit()
|
|
# count = cursor.rowcount
|
|
# print(count, "Record inserted successfully into mobile table")
|
|
|
|
except (Exception, psycopg2.Error) as error:
|
|
print("Failed to insert record into mobile table", error)
|
|
return False
|
|
|
|
# finally:
|
|
# # closing database connection.
|
|
# if connection:
|
|
# connection.cursor().close()
|
|
# connection.close()
|
|
# print("PostgreSQL connection is closed")
|
|
|