我尝试使用以下架构查询GeoIP数据库:
CREATE TABLE api_geoiprecord
(
start_ip inet PRIMARY KEY NOT NULL,
end_ip inet NOT NULL,
region VARCHAR(2),
country VARCHAR(2),
city VARCHAR(50),
postal_code VARCHAR(50),
latitude DOUBLE PRECISION,
longitude DOUBLE PRECISION,
dma_code INT,
area_code INT
);
当我在psql
中运行此查询时,我得到了结果:
-- Test address, belongs to Yahoo!
SELECT city, country from api_geoiprecord
WHERE inet '68.180.194.242' BETWEEN start_ip AND end_ip
LIMIT 1;
city | country
-----------+---------
Sunnyvale | CA
(1 row)
当我在Django中运行时,我什么都没得到:
def get_location(ip) -> None:
cur = connection.cursor()
cur.execute(
"""SELECT city, country FROM api_geoiprecord
WHERE inet %s BETWEEN start_ip AND end_ip
LIMIT 1;""",
(ip, )
)
return cur.fetchone()
有什么想法?