浏览代码

New class : data consumer

Jovian (Netbook) 6 年之前
父节点
当前提交
166400a756
共有 2 个文件被更改,包括 27 次插入8 次删除
  1. 14 0
      app_dispatcher.py
  2. 13 8
      connect.py

+ 14 - 0
app_dispatcher.py

@@ -0,0 +1,14 @@
+import connect.py
+
+credentials = "host=appli-pfe.ec-nantes.fr dbname=app_bdonn_01 user=app2_01 password=osiris"
+
+print('Start')
+
+consumer = DataConsumer(credentials)
+
+rows = consumer.getProductSet()
+
+for r in rows:
+	print(r)
+
+print('End')

+ 13 - 8
connect.py

@@ -1,15 +1,20 @@
 import psycopg2
 
-conn = psycopg2.connect("host=appli-pfe.ec-nantes.fr dbname=app_bdonn_01 user=app2_01 password=osiris")
+class DataConsumer():
+	"""To query database"""
+	def __init__(self, credentials):
+		self.conn = psycopg2.connect(credentials)
 
-cursor = conn.cursor()
+	def __del__(self):
+		conn.close()
 
-cursor.execute("SELECT * from Product")
+	def getProductSet(self):
+		cursor = conn.cursor()
 
-rows = cursor.fetchall()
+		cursor.execute("SELECT * from Product")
 
-for row in rows:
-	print(row)
+		rows = cursor.fetchall()
 
-cursor.close()
-conn.close()
+		cursor.close()
+
+		return rows