Running Updates row by row, beginners Q
Hello all,
I am extremely new to MySQL and Python, which will be evident soon enough. I have a script that _should_ geocode a number of addresses using Geopy, then return the results with an individual latitude and longitude for each row.
Something is goofy with the part that's actually running the SQL, I think. While Geopy is effectively returning results, the UPDATE portion is changing ALL of the latitude and longitude fields to reflect the last row.
Anyway, here's my script. How do I run the update for each row, instead of for all rows?
import MySQLdb
import simplejson
from geopy import geocoders
g = geocoders.Google('APICODE')
conn = MySQLdb.connect(user='root', db='log', passwd='pass')
cursor = conn.cursor()
cursor.execute('SELECT address from log.table')
results = cursor.fetchall()
for row in results:
location = "%s, City, state" % (row[0])
for place, (lat, lng) in g.geocode(location, exactly_one=False):
cursor.execute(("""UPDATE tableSET latitude =%s, longitude = %s"""), (lat, lng))
cursor.close()
conn.commit
conn.close()
Any advice would be greatly appreciated. Thanks!
-Matt