Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added __pycache__/config.cpython-38.pyc
Binary file not shown.
1 change: 1 addition & 0 deletions company.pgerd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"version":"61500","data":{"id":"6b886d70-1b46-4ee5-b163-52f17591d3c1","offsetX":295.3482927072546,"offsetY":244.22019388975247,"zoom":85.41666666666669,"gridSize":15,"layers":[{"id":"192b0d3a-c6e4-4f9f-b76e-e4d9bf008746","type":"diagram-links","isSvg":true,"transformed":true,"models":{"0260359a-5c41-470a-8d2a-e5bf654c16b2":{"id":"0260359a-5c41-470a-8d2a-e5bf654c16b2","locked":true,"type":"onetomany","selected":false,"source":"370c8c51-2312-4692-bd6a-3ec264a9f2d8","sourcePort":"8860e36a-bd7e-4061-8ae2-34b4259902a0","target":"096a9336-7639-4410-89db-bb2765b5c1a3","targetPort":"f539080f-e476-4a52-86d8-06bc813d87cc","points":[{"id":"2dc01d0e-09b3-4341-8976-d8c38f22f931","type":"point","x":94.9999813465373,"y":-162.99999928708823},{"id":"c9d8a55b-0543-4f80-9984-9359efe029b9","type":"point","x":134.22638902565313,"y":-162.99999928708823},{"id":"ac4e3dbe-50d2-4d29-b737-5b70c5c32d85","type":"point","x":134.22638902565313,"y":212.00002323262606},{"id":"dbdc5c1b-5506-424f-8290-e4a27ea6d875","type":"point","x":35.00000553706582,"y":212.00002323262606}],"labels":[],"width":1,"color":"gray","curvyness":50,"selectedColor":"rgb(0,192,255)","data":{"local_table_uid":"096a9336-7639-4410-89db-bb2765b5c1a3","local_column_attnum":0,"referenced_table_uid":"370c8c51-2312-4692-bd6a-3ec264a9f2d8","referenced_column_attnum":0}},"085c4ccc-21be-4fb1-9b9f-849cdfe7d73c":{"id":"085c4ccc-21be-4fb1-9b9f-849cdfe7d73c","locked":true,"type":"onetomany","source":"3dcc61af-2e76-46a6-8fea-e3835a5715cd","sourcePort":"6858df81-4699-4257-b48e-dfd27b4a8d8a","target":"89c74277-e49c-40d1-9841-75fd0c1fb94f","targetPort":"ee540033-2c8f-43d2-8913-17d24dc55d31","points":[{"id":"8066da97-e828-4d39-9fe4-b401cf90f965","type":"point","x":620.0000055370658,"y":347.00002323262606},{"id":"3b34db2d-a791-4529-a181-9947f98f1214","type":"point","x":620.0000055370658,"y":-117.99999928708826},{"id":"d823fa3f-1bdd-47a8-a501-763f376dade6","type":"point","x":439.9999813465374,"y":-117.99999928708826}],"labels":[],"width":1,"color":"gray","curvyness":50,"selectedColor":"rgb(0,192,255)","data":{"local_table_uid":"89c74277-e49c-40d1-9841-75fd0c1fb94f","local_column_attnum":0,"referenced_table_uid":"3dcc61af-2e76-46a6-8fea-e3835a5715cd","referenced_column_attnum":1}},"821d9815-afe7-4cf8-b726-019e061099b2":{"id":"821d9815-afe7-4cf8-b726-019e061099b2","locked":true,"type":"onetomany","selected":false,"source":"89c74277-e49c-40d1-9841-75fd0c1fb94f","sourcePort":"ee540033-2c8f-43d2-8913-17d24dc55d31","target":"096a9336-7639-4410-89db-bb2765b5c1a3","targetPort":"f539080f-e476-4a52-86d8-06bc813d87cc","points":[{"id":"cebe3a13-731f-4e27-8c8a-acfb77da9051","type":"point","x":439.9999813465374,"y":-117.99999928708826},{"id":"1309cfa2-2439-42a6-b1d8-fa72f1f48fd3","type":"point","x":439.9999813465374,"y":212.00002323262606},{"id":"1c68d6fc-4806-4b21-866b-822742f9cf38","type":"point","x":35.00000553706582,"y":212.00002323262606}],"labels":[],"width":1,"color":"gray","curvyness":50,"selectedColor":"rgb(0,192,255)","data":{"local_table_uid":"096a9336-7639-4410-89db-bb2765b5c1a3","local_column_attnum":0,"referenced_table_uid":"89c74277-e49c-40d1-9841-75fd0c1fb94f","referenced_column_attnum":0}}}},{"id":"c2355e13-7bd8-44ee-859b-3f90f16d8e0e","type":"diagram-nodes","isSvg":false,"transformed":true,"models":{"370c8c51-2312-4692-bd6a-3ec264a9f2d8":{"id":"370c8c51-2312-4692-bd6a-3ec264a9f2d8","type":"table","selected":false,"x":-105,"y":-255,"ports":[{"id":"fd9c0cf4-978a-45d8-8c3b-c0aad97516fc","type":"onetomany","x":65.00000553706593,"y":-57.99997676737388,"name":"coll-port-3","alignment":"right","parentNode":"370c8c51-2312-4692-bd6a-3ec264a9f2d8","links":[],"subtype":"many"},{"id":"ed53bd3e-e26c-4c8e-9018-4d725953d77d","type":"onetomany","x":65.00001261360576,"y":-132.00000073915265,"name":"coll-port-1","alignment":"right","parentNode":"370c8c51-2312-4692-bd6a-3ec264a9f2d8","links":[],"subtype":"many"},{"id":"8860e36a-bd7e-4061-8ae2-34b4259902a0","type":"onetomany","x":64.9999813465373,"y":-162.99999928708823,"name":"coll-port-0","alignment":"right","parentNode":"370c8c51-2312-4692-bd6a-3ec264a9f2d8","links":["0260359a-5c41-470a-8d2a-e5bf654c16b2"],"subtype":"many"}],"name":"Untitled","color":"rgb(0,192,255)","portsInOrder":[],"portsOutOrder":["fd9c0cf4-978a-45d8-8c3b-c0aad97516fc","ed53bd3e-e26c-4c8e-9018-4d725953d77d","8860e36a-bd7e-4061-8ae2-34b4259902a0"],"otherInfo":{"data":{"name":"teams","coll_inherits":[],"hastoasttable":true,"toast_autovacuum_enabled":"x","autovacuum_enabled":"x","primary_key":[{"columns":[{"column":"teamID","cid":"c580"}],"include":[],"cid":"c579"}],"foreign_key":[],"partition_keys":[],"partitions":[],"partition_type":"range","is_partitioned":false,"columns":[{"name":"teamID","is_primary_key":true,"attnum":0,"cltype":"integer","attlen":null,"attprecision":null,"collspcname":null,"attnotnull":true,"colconstype":"n","attidentity":"a","attoptions":[],"cid":"c583"},{"name":"teamName","is_primary_key":false,"attnum":1,"cltype":"character varying","attlen":null,"attprecision":null,"attnotnull":false,"colconstype":"n","attidentity":"a","attoptions":[],"cid":"c584","min_val_attlen":1,"max_val_attlen":2147483647},{"name":"teamDepartment","is_primary_key":false,"attnum":2,"cltype":"character varying","attlen":null,"attprecision":null,"attnotnull":false,"colconstype":"n","attidentity":"a","attoptions":[],"cid":"c585","min_val_attlen":1,"max_val_attlen":2147483647},{"name":"teamproduct","is_primary_key":false,"attnum":3,"cltype":"character varying","attlen":null,"attprecision":null,"attnotnull":false,"colconstype":"n","attidentity":"a","attoptions":[],"cid":"c586","min_val_attlen":1,"max_val_attlen":2147483647}],"schema":"public"},"note":"","metadata":{"data_failed":false,"is_promise":false,"fillColor":null,"textColor":null}}},"89c74277-e49c-40d1-9841-75fd0c1fb94f":{"id":"89c74277-e49c-40d1-9841-75fd0c1fb94f","type":"table","selected":false,"x":240,"y":-210,"ports":[{"id":"ee540033-2c8f-43d2-8913-17d24dc55d31","type":"onetomany","x":409.9999813465374,"y":-117.99999928708826,"name":"coll-port-0","alignment":"right","parentNode":"89c74277-e49c-40d1-9841-75fd0c1fb94f","links":["085c4ccc-21be-4fb1-9b9f-849cdfe7d73c","821d9815-afe7-4cf8-b726-019e061099b2"],"subtype":"one"},{"id":"e62ecca4-06fe-491c-9aaf-323868263e01","type":"onetomany","x":409.9999813465374,"y":-86.99997546849073,"name":"coll-port-1","alignment":"right","parentNode":"89c74277-e49c-40d1-9841-75fd0c1fb94f","links":[],"subtype":"one"},{"id":"2dd9ff65-776c-4c8a-b818-71ccfbfbddc4","type":"onetomany","x":409.9999813465374,"y":-18.999999287088315,"name":"coll-port-3","alignment":"right","parentNode":"89c74277-e49c-40d1-9841-75fd0c1fb94f","links":[],"subtype":"one"}],"name":"Untitled","color":"rgb(0,192,255)","portsInOrder":[],"portsOutOrder":["ee540033-2c8f-43d2-8913-17d24dc55d31","e62ecca4-06fe-491c-9aaf-323868263e01","2dd9ff65-776c-4c8a-b818-71ccfbfbddc4"],"otherInfo":{"data":{"name":"products","coll_inherits":[],"hastoasttable":true,"toast_autovacuum_enabled":"x","autovacuum_enabled":"x","primary_key":[{"columns":[{"column":"productID","cid":"c856"}],"include":[],"cid":"c855"}],"foreign_key":[{"confmatchtype":false,"autoindex":false,"columns":[{"local_column":"productID","referenced":"customerID","references":"3dcc61af-2e76-46a6-8fea-e3835a5715cd","references_table_name":"(public) customer","cid":"c881"}],"confupdtype":"a","confdeltype":"a","cid":"c863"}],"partition_keys":[],"partitions":[],"partition_type":"range","is_partitioned":false,"columns":[{"name":"productID","is_primary_key":true,"attnum":0,"cltype":"integer","attlen":null,"attprecision":null,"collspcname":null,"attnotnull":true,"colconstype":"n","attidentity":"a","attoptions":[],"cid":"c857"},{"name":"teamID","is_primary_key":false,"attnum":3,"cltype":"integer","attlen":null,"attprecision":null,"collspcname":null,"attnotnull":false,"colconstype":"n","attidentity":"a","attoptions":[],"cid":"c858"},{"name":"productName","is_primary_key":false,"attnum":1,"cltype":"character varying","attlen":null,"attprecision":null,"attnotnull":false,"colconstype":"n","attidentity":"a","attoptions":[],"cid":"c859","min_val_attlen":1,"max_val_attlen":2147483647},{"name":"productCustomer","is_primary_key":false,"attnum":2,"cltype":"character varying","attlen":null,"attprecision":null,"attnotnull":false,"colconstype":"n","attidentity":"a","attoptions":[],"cid":"c860","min_val_attlen":1,"max_val_attlen":2147483647}],"schema":"public"},"note":"","metadata":{"data_failed":false,"is_promise":false,"fillColor":null,"textColor":null}}},"096a9336-7639-4410-89db-bb2765b5c1a3":{"id":"096a9336-7639-4410-89db-bb2765b5c1a3","type":"table","selected":false,"x":-165,"y":120,"ports":[{"id":"f539080f-e476-4a52-86d8-06bc813d87cc","type":"onetomany","x":5.000005537065817,"y":212.00002323262606,"name":"coll-port-0","alignment":"right","parentNode":"096a9336-7639-4410-89db-bb2765b5c1a3","links":["0260359a-5c41-470a-8d2a-e5bf654c16b2","821d9815-afe7-4cf8-b726-019e061099b2"],"subtype":"one"}],"name":"Untitled","color":"rgb(0,192,255)","portsInOrder":[],"portsOutOrder":["f539080f-e476-4a52-86d8-06bc813d87cc"],"otherInfo":{"data":{"name":"Employee","coll_inherits":[],"hastoasttable":true,"toast_autovacuum_enabled":"x","autovacuum_enabled":"x","primary_key":[{"columns":[{"column":"EmployeeID","cid":"c896"}],"include":[],"cid":"c895"}],"foreign_key":[{"confmatchtype":false,"autoindex":false,"columns":[{"local_column":"EmployeeID","referenced":"teamID","references":"370c8c51-2312-4692-bd6a-3ec264a9f2d8","references_table_name":"(public) teams","cid":"c898"}],"confupdtype":"a","confdeltype":"a","cid":"c897"},{"confmatchtype":false,"autoindex":false,"columns":[{"local_column":"EmployeeID","referenced":"productID","references_table_name":"products","references":"89c74277-e49c-40d1-9841-75fd0c1fb94f"}],"confupdtype":"a","confdeltype":"a"}],"partition_keys":[],"partitions":[],"partition_type":"range","is_partitioned":false,"columns":[{"name":"EmployeeID","is_primary_key":true,"attnum":0,"cltype":"integer","attlen":null,"attprecision":null,"collspcname":null,"attnotnull":true,"colconstype":"n","attidentity":"a","attoptions":[],"cid":"c899"},{"name":"EmployeeFullName","is_primary_key":false,"attnum":1,"cltype":"character varying","attlen":null,"attprecision":null,"attnotnull":false,"colconstype":"n","attidentity":"a","attoptions":[],"cid":"c900","min_val_attlen":1,"max_val_attlen":2147483647},{"name":"EmployeeTitle","is_primary_key":false,"attnum":3,"cltype":"character varying","attlen":null,"attprecision":null,"attnotnull":false,"colconstype":"n","attidentity":"a","attoptions":[],"cid":"c901","min_val_attlen":1,"max_val_attlen":2147483647},{"name":"EmployeeTeam","is_primary_key":false,"attnum":4,"cltype":"character varying","attlen":null,"attprecision":null,"attnotnull":false,"colconstype":"n","attidentity":"a","attoptions":[],"cid":"c902","min_val_attlen":1,"max_val_attlen":2147483647},{"name":"EmployeeDateofBirth","is_primary_key":false,"attnum":2,"cltype":"date","attlen":null,"attprecision":null,"collspcname":null,"attnotnull":false,"colconstype":"n","attidentity":"a","attoptions":[],"cid":"c903"}],"schema":"public"},"note":"","metadata":{"data_failed":false,"is_promise":false,"fillColor":null,"textColor":null}}},"3dcc61af-2e76-46a6-8fea-e3835a5715cd":{"id":"3dcc61af-2e76-46a6-8fea-e3835a5715cd","type":"table","selected":true,"x":420,"y":255,"ports":[{"id":"6858df81-4699-4257-b48e-dfd27b4a8d8a","type":"onetomany","x":590.0000055370658,"y":347.00002323262606,"name":"coll-port-1","alignment":"right","parentNode":"3dcc61af-2e76-46a6-8fea-e3835a5715cd","links":["085c4ccc-21be-4fb1-9b9f-849cdfe7d73c"],"subtype":"many"}],"name":"Untitled","color":"rgb(0,192,255)","portsInOrder":[],"portsOutOrder":["6858df81-4699-4257-b48e-dfd27b4a8d8a"],"otherInfo":{"data":{"columns":[{"name":"customerID","is_primary_key":true,"attnum":1,"cltype":"integer","attlen":null,"attprecision":null,"collspcname":null,"attnotnull":true,"colconstype":"n","attidentity":"a","attoptions":[],"cid":"c95"},{"name":"customerName","is_primary_key":false,"attnum":2,"cltype":"character varying","attlen":null,"attprecision":null,"attnotnull":false,"colconstype":"n","attidentity":"a","attoptions":[],"cid":"c96","min_val_attlen":1,"max_val_attlen":2147483647},{"name":"customerDomain","is_primary_key":false,"attnum":0,"cltype":"character varying","attlen":null,"attprecision":null,"attnotnull":false,"colconstype":"n","attidentity":"a","attoptions":[],"cid":"c94","min_val_attlen":1,"max_val_attlen":2147483647}],"name":"customer","coll_inherits":[],"hastoasttable":true,"toast_autovacuum_enabled":"x","autovacuum_enabled":"x","primary_key":[{"columns":[{"column":"customerID"}],"include":[]}],"foreign_key":[],"partition_keys":[],"partitions":[],"partition_type":"range","is_partitioned":false,"schema":"public"},"note":"","metadata":{"data_failed":false,"is_promise":false,"fillColor":null,"textColor":null}}}}}]}}
20 changes: 20 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/python
from configparser import ConfigParser

def config(filename, section):
# create a parser
parser = ConfigParser()
# read config file
parser.read(filename)

# get section, default to postgresql
db = {} #empty dictionary
if parser.has_section(section):
params = parser.items(section) #params is a list of tuples
# params = [('host', 'localhost'), ('database', 'Chinook_PostgreSql'), ('user', 'postgres'), ('password', 'postgres')]
for param in params: #param is a tuple (0,1)>>>> param= ('host', 'localhost')
db[param[0]] = param[1] #dict_name[key]=value
else:
raise Exception('Section {0} not found in the {1} file'.format(section,filename))
# db = {'host': 'localhost', 'database': 'Chinook_PostgreSql', 'user': 'postgres', 'password': 'postgres'}
return db
54 changes: 54 additions & 0 deletions connecttoDB.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/python
import psycopg2
from config import config


def connect():
""" Connect to the PostgreSQL database server """
conn = None
try:
# read connection parameters
params = config("database.ini","learn_pagila")
#params= {'host': 'localhost', 'database': 'Chinook_PostgreSql', 'user': 'postgres', 'password': 'postgres'}

# connect to the PostgreSQL server
print('Connecting to the PostgreSQL database...')
conn = psycopg2.connect(**params)
conn.autocommit = True
# create a cursor
cur = conn.cursor()

# execute a statement
# file = open('sqlcommands.sql', 'r')
# sqlFile = file.read()
# file.close()
# sqlCommands = sqlFile.split(';')[:-1]
# for command in sqlCommands:
# try:
# cur.execute(command)
# print(cur.fetchall())
# except (Exception, psycopg2.DatabaseError) as error:
# print("Command skipped: ", error)
# break


cur.execute('SELECT * from actor;')
print(cur.fetchall())
cur.execute('SELECT * FROM category;')
print(cur.fetchone())
cur.execute('SELECT * from address;')
print(cur.fetchmany(50))



# close the communication with the PostgreSQL
cur.close()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
print('Database connection closed.')

if __name__ == '__main__':
connect()
20 changes: 20 additions & 0 deletions database.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[learn_pagila]
host=localhost
database=learn_pagila
user=postgres
password=postgres


[PyCoders]
host=localhost
database=PyCoders
user=postgres
password=postgres



; conn = psycopg2.connect(
; host="localhost",
; database="Chinook_PostgreSql",
; user="postgres",
; password="1234")
10 changes: 10 additions & 0 deletions ex5sqlcommands.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CREATE TABLE students (student_id int,FirstName varchar(255), PRIMARY KEY (student_id));
CREATE TABLE teachers (teacher_id int,FirstName varchar(255), PRIMARY KEY (teacher_id));

INSERT INTO students (student_id, FirstName) VALUES (4006, 'Rama ');
INSERT INTO students (student_id, FirstName) VALUES (4005, 'rima ');
INSERT INTO students (student_id, FirstName) VALUES (4004, 'sobhi ');

INSERT INTO teachers (teacher_id, FirstName) VALUES (11, 'x ');
INSERT INTO teachers (teacher_id, FirstName) VALUES (12, 'xy');
INSERT INTO teachers (teacher_id, FirstName) VALUES (13, 'xyz ');
50 changes: 50 additions & 0 deletions exercise5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/python
import psycopg2
from config import config


def connect():
""" Connect to the PostgreSQL database server """
conn = None
try:
# read connection parameters
params = config("database.ini", "PyCoders")
# connect to the PostgreSQL server
print('Connecting to the PyCoders database...')
conn = psycopg2.connect(**params)
conn.autocommit = True
# create a cursor
cur = conn.cursor()

file = open('ex5sqlcommands.sql', 'r')
sqlFile = file.read()
file.close()
sqlCommands = sqlFile.split(';')[:-1]
for command in sqlCommands:
try:
cur.execute(command)
# print(cur.fetchall())
except (Exception, psycopg2.DatabaseError) as error:
print("Command skipped because of the error: ", error)
break


sql_select_query='SELECT * FROM students'
cur.execute(sql_select_query)
print(cur.fetchall())

sql_select_query='SELECT * FROM teachers'
cur.execute(sql_select_query)
print(cur.fetchall())
cur.close()


except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
print('Database connection closed.')

if __name__ == '__main__':
connect()
Loading