diff --git a/week09/ex1.jpg b/week09/ex1.jpg new file mode 100644 index 0000000..ee7c8f9 Binary files /dev/null and b/week09/ex1.jpg differ diff --git a/week09/ex1.sql b/week09/ex1.sql new file mode 100644 index 0000000..0789686 --- /dev/null +++ b/week09/ex1.sql @@ -0,0 +1,10 @@ +CREATE TYPE addr as (address VARCHAR(255), city_id int); +CREATE OR REPLACE FUNCTION get_address_and_city_for_ex1() +RETURNS TABLE (addr text) AS $$ +DECLARE +BEGIN + return QUERY SELECT CONCAT(address, ', ', CAST(city_id as VARCHAR)) from address where address like '%11%' and city_id >= 400 and city_id <= 600; +END; +$$ LANGUAGE plpgsql; + + diff --git a/week09/ex2.sql b/week09/ex2.sql new file mode 100644 index 0000000..55f0292 --- /dev/null +++ b/week09/ex2.sql @@ -0,0 +1,11 @@ +CREATE OR REPLACE FUNCTION retrievecustomers(l int, r int) +RETURNS TABLE (customer_id int) AS $$ +DECLARE +BEGIN + IF l >= 0 OR l <= 600 OR r >= 0 OR r <= 600 THEN + return QUERY SELECT customer.customer_id FROM customer ORDER BY address_id LIMIT (r-l) OFFSET l; + ELSE + RAISE '% %', l, r; + END IF; +END; +$$ LANGUAGE plpgsql; diff --git a/week09/lol.py b/week09/lol.py new file mode 100644 index 0000000..8a850d6 --- /dev/null +++ b/week09/lol.py @@ -0,0 +1,31 @@ +import psycopg2 +import time +from geopy.geocoders import Nominatim + +con = psycopg2.connect(database="dvdrental", user="postgres", + password="postgres", host="127.0.0.1", port="5432") + +geolocator = Nominatim(user_agent="curl") + +cur = con.cursor() +cur.execute('ALTER TABLE address ADD COLUMN IF NOT EXISTS latitude VARCHAR(255) DEFAULT (\'0\');') +cur.execute('ALTER TABLE address ADD COLUMN IF NOT EXISTS longitude VARCHAR(255) DEFAULT (\'0\');') +con.commit() +cur.callproc('get_address_and_city_for_ex1') +rows = cur.fetchall() +for row in rows: + print(row[0]) + location = geolocator.geocode(row[0], timeout=None) + lat = 0 + lon = 0 + if location is not None: + print(location.address) + print((location.latitude, location.longitude)) + lat = location.latitude + lon = location.longitude + + addr = row[0].split(', ') + cur.execute(f'UPDATE address SET latitude=\'{lat}\', longitude=\'{lon}\' WHERE address=\'{addr[0]}\' and city_id=\'{addr[1]}\'') + time.sleep(1) + +con.commit()