我正在尝试制作一个简单的地图制作功能。我有一个包含地理坐标(“纬度”和“经度”),地名(“ feature_names”)和ISO-3166国家/地区代码(“ countrycodes”)的数据库。现在,我已经可以通过查询纬度/经度并让Folium制作我的地图来工作了,但是问题是,世界上有多个渥太华,温哥华,伦敦等。因此,引入了国家/地区代码以尝试缩小范围。
我的问题是我似乎无法使SQL查询返回任何内容。代码执行时不会抛出错误,但不会返回任何内容。 TL; DR:如何对数据库进行多个查询?
cur.execute("SELECT feature_name FROM WorldCities")
locnames = str(cur.fetchall())
cur.execute("SELECT country_code FROM WorldCities")
countrycodes = str(cur.fetchall())
def mapmaker():
location = input(prompt = "Where would you like to go? ")
country = input(prompt = "Great! Do you know which country this is in? ")
if not location:
input(prompt = "Sorry, I didn't quite get that")
if location in locnames and country in countrycodes:
#latquery = "SELECT latitude FROM WorldCities WHERE feature_name = ? AND country_code = ?"
#lat = cur.execute(latquery, (location, ))
latquery = cur.execute("SELECT latitude FROM WorldCities WHERE feature_name = ? AND country_code = ?", ((location,) (countrycodes,)))
#lat = cur.execute(latquery, (location, + countrycodes))
latloc = (latquery.fetchall())
#longquery = "SELECT longitude FROM WorldCities WHERE feature_name = ?"
#long = cur.execute(longquery, (location, ))
#longloc = (long.fetchone())
print(latloc) #longloc[0], country[0])
#map = folium.Map(location=(latloc[0], longloc[0]), zoom_start=15)
#return map
else:
print ("Sorry, I didn't understand your query")
谢谢
答案 0 :(得分:0)
数据库比简单的简单行列表更聪明。特别是,它可以包含索引以加速查询。如果不知道是否以及如何为数据库建立索引,就不可能优化查询。
如果数据库包含索引,则不应将其内容加载到平面列表中,而应使用直接查询。
话虽如此,您可以从一个查询中检索多个列,所以恕我直言,从表中提取地理坐标的正确方法应该是:
latlonquery = cur.execute("""SELECT latitude, longitude FROM WorldCities
WHERE feature_name = ? AND country_code = ?""", (location, countrycodes))
lat_lon = latlonquery.fetchone()
if lat_lon is not None:
lat, lon = lat_lon
print(lat, lon, location, country)
...