MySQL Forums
Forum List  »  Connector/Python

Running Updates row by row, beginners Q
Posted by: Henry LaForge
Date: September 19, 2007 02:10PM

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

Options: ReplyQuote


Subject
Written By
Posted
Running Updates row by row, beginners Q
September 19, 2007 02:10PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.