Browse Source

week9

master
RinRi 1 year ago
parent
commit
45de658043
4 changed files with 52 additions and 0 deletions
  1. BIN
      week09/ex1.jpg
  2. +10
    -0
      week09/ex1.sql
  3. +11
    -0
      week09/ex2.sql
  4. +31
    -0
      week09/lol.py

BIN
week09/ex1.jpg View File

Before After
Width: 1920  |  Height: 1080  |  Size: 351 KiB

+ 10
- 0
week09/ex1.sql View File

@@ -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;



+ 11
- 0
week09/ex2.sql View File

@@ -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;

+ 31
- 0
week09/lol.py View File

@@ -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()

Loading…
Cancel
Save