def get_by_id(self, customer: CustomerEntity): con = None try: con = psycopg2.connect(user=self.ConnectionData['user'], password=self.ConnectionData['password'], host=self.ConnectionData['host'], port=self.ConnectionData['port'], database=self.ConnectionData['database']) cur = con.cursor() sql = "SELECT * FROM TblCustomers WHERE customerid=%s" cur.execute(sql, (customer.CustomerID, )) con.commit() row = cur.fetchone() if row: c = CustomerEntity() c.fetch_data(row) return c, 200 con.close() return "Customer ID not found", 404 except (Exception, psycopg2.DatabaseError) as error: return str(error) finally: if con is not None: con.close()
def get_one_Customer(self, customer: CustomerEntity): conn = psycopg2.connect(host=self.connection_data['host'], port=self.connection_data['port'], user=self.connection_data['user'], password=self.connection_data['password'], database=self.connection_data['database']) cursor = conn.cursor() sql = "SELECT * FROM customers Where customerid=%s " cursor.execute(sql, ((customer.customer_id, ))) conn.commit() row = cursor.fetchone() # result = {} # for row in rows: customer = CustomerEntity() customer.fetch_data(row) # result.append(customer.to_json()) return customer.to_json()
def get_all(self): con = None try: con = psycopg2.connect(user=self.ConnectionData['user'], password=self.ConnectionData['password'], host=self.ConnectionData['host'], port=self.ConnectionData['port'], database=self.ConnectionData['database']) cur = con.cursor() sql = "SELECT * FROM TblCustomers" cur.execute(sql) con.commit() rows = cur.fetchall() result = [] for row in rows: c = CustomerEntity() c.fetch_data(row) result.append(c.to_json()) con.close() return result except (Exception, psycopg2.DatabaseError) as error: return str(error) finally: if con is not None: con.close()
def get_all_Customer(self): conn = psycopg2.connect(host=self.connection_data['host'], port=self.connection_data['port'], user=self.connection_data['user'], password=self.connection_data['password'], database=self.connection_data['database']) cursor = conn.cursor() sql = "SELECT * FROM customers" cursor.execute(sql) conn.commit() rows = cursor.fetchall() result = [] for row in rows: customer = CustomerEntity() customer.fetch_data(row) result.append(customer.to_json()) return result