all the project copy
This commit is contained in:
42
api/connection.py
Normal file
42
api/connection.py
Normal file
@@ -0,0 +1,42 @@
|
||||
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")
|
||||
|
||||
Reference in New Issue
Block a user