瀏覽代碼

week9

master
RinRi 1 年之前
父節點
當前提交
45de658043
共有 4 個檔案被更改,包括 52 行新增0 行删除
  1. 二進制
      week09/ex1.jpg
  2. +10
    -0
      week09/ex1.sql
  3. +11
    -0
      week09/ex2.sql
  4. +31
    -0
      week09/lol.py

二進制
week09/ex1.jpg 查看文件

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

+ 10
- 0
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;



+ 11
- 0
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;

+ 31
- 0
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()

Loading…
取消
儲存