Пример #1
0
    def get_by_id(self, categories: CategoriesEntity):
        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 Categories WHERE CategoryID= %s"
            cur.execute(sql, (categories.CategoryID, ))
            con.commit()
            row = cur.fetchone()
            if row:
                c = CategoriesEntity()
                c.fetch_data(row)
                return c, 200
            con.close()
            return 'CategoryID not found', 404

        except (Exception, psycopg2.DatabaseError) as error:
            return str(error)
        finally:
            if con is not None:
                con.close()
Пример #2
0
 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"
         sql = "SELECT * FROM Categories"
         cur.execute(sql)
         con.commit()
         rows = cur.fetchall()
         result = []
         for row in rows:
             c = CategoriesEntity()
             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()