beautified
This commit is contained in:
parent
c1cb18319c
commit
5a32c980ad
1 changed files with 24 additions and 20 deletions
|
@ -1,10 +1,3 @@
|
|||
# assign environment variable DB_HOST to variable host
|
||||
# assign environment variable DB_NAME to variable database
|
||||
# assign environment variable DB_USER to variable db_user
|
||||
# assign environment variable DB_PASSWORD to variable db_password
|
||||
# assign environment variable DB_PORT to variable db_port
|
||||
|
||||
|
||||
# establish connection to database
|
||||
import os
|
||||
import psycopg2
|
||||
|
@ -18,14 +11,21 @@ password = os.environ['DB_PASSWORD']
|
|||
port = os.environ['DB_PORT']
|
||||
|
||||
|
||||
conn = psycopg2.connect(host=host, database=database, user=user, password=password, port=port)
|
||||
conn = psycopg2.connect(
|
||||
host=host,
|
||||
database=database,
|
||||
user=user,
|
||||
password=password,
|
||||
port=port)
|
||||
conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
|
||||
cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
|
||||
|
||||
# check if column finder exists in table bird_fallenbird
|
||||
cur.execute("SELECT column_name FROM information_schema.columns WHERE table_name = 'bird_fallenbird' AND column_name = 'finder'")
|
||||
cur.execute("SELECT column_name FROM information_schema.columns WHERE
|
||||
table_name = 'bird_fallenbird' AND column_name = 'finder'")
|
||||
|
||||
# if column finder does not exist in table bird_fallenbird, add column finder to table bird_fallenbird
|
||||
# if column finder does not exist in table bird_fallenbird, add column
|
||||
# finder to table bird_fallenbird
|
||||
if not cur.fetchone():
|
||||
cur.execute("alter table bird_fallenbird add column finder text")
|
||||
|
||||
|
@ -33,14 +33,18 @@ if not cur.fetchone():
|
|||
cur.execute("SELECT * FROM rescuer_rescuer")
|
||||
rescuer_rescuer = cur.fetchall()
|
||||
|
||||
# query the table bird_fallenbird with its columns id and rescuer_id and write the results to a variable
|
||||
# query the table bird_fallenbird with its columns id and rescuer_id and
|
||||
# write the results to a variable
|
||||
cur.execute("SELECT id, rescuer_id FROM bird_fallenbird")
|
||||
bird_fallenbird = cur.fetchall()
|
||||
|
||||
# iterate over the results of the query bird_fallenbird
|
||||
# for each row, iterate over the results of the query rescuer_rescuer
|
||||
# if the rescuer_id of the bird_fallenbird row matches the id of the rescuer_rescuer row, update the bird_fallenbird row finder with all the data of the rescuer_rescuer
|
||||
# all rows of rescuer_rescuer should be inserted into the column finder of bird_fallenbird with the same rescuer_id
|
||||
# if the rescuer_id of the bird_fallenbird row matches the id of the
|
||||
# rescuer_rescuer row, update the bird_fallenbird row finder with
|
||||
# all the data of the rescuer_rescuer
|
||||
# all rows of rescuer_rescuer should be inserted into the column finder of
|
||||
# bird_fallenbird with the same rescuer_id
|
||||
for row in bird_fallenbird:
|
||||
for rescuer in rescuer_rescuer:
|
||||
if row['rescuer_id'] == rescuer['id']:
|
||||
|
@ -56,17 +60,17 @@ for row in bird_fallenbird:
|
|||
elif key == 'last_name':
|
||||
key = 'Nachname'
|
||||
elif key == 'phone':
|
||||
key = 'Telefonnummer'
|
||||
key = 'Telefonnummer'
|
||||
elif key == 'street':
|
||||
key = 'Straße'
|
||||
key = 'Straße'
|
||||
elif key == 'street_number':
|
||||
key = 'Hausnummer'
|
||||
key = 'Hausnummer'
|
||||
elif key == 'city':
|
||||
key = 'Stadt'
|
||||
key = 'Stadt'
|
||||
elif key == 'zip_code':
|
||||
key = 'PLZ'
|
||||
finder += key + ': ' + str(value) + '\n'
|
||||
finder = finder[:-2]
|
||||
cur.execute("UPDATE bird_fallenbird SET finder = %s WHERE id = %s", (finder, row['id']))
|
||||
|
||||
|
||||
cur.execute(
|
||||
"UPDATE bird_fallenbird SET finder = %s WHERE id = %s",
|
||||
(finder, row['id']))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue