Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a table called coords and it is defined as:

mysql> describe coords;
+-----------+--------------+------+-----+------------------+----------------+
| Field     | Type         | Null | Key | Default          | Extra          |
+-----------+--------------+------+-----+------------------+----------------+
| id        | int(11)      | NO   | PRI | NULL             | auto_increment |
| location  | varchar(150) | NO   | UNI | NULL             |                |
| latitude  | float(20,14) | YES  |     | 0.00000000000000 |                |
| longitude | float(20,14) | YES  |     | 0.00000000000000 |                |
-----------------------------------------------------------------------------

I am using the MySQLdb import in my Python script. The purpose of this table is to store (as you can guess, but for clarity) location coordinates (but only when I do not have the coordinates already for a particular location).

I will be querying this table in my Python program to see if I already have coordinates for a pre-requested location. I'm doing this to speed up the use of the geopy package that interrogates Google's Geolocation Service.

How do I store the returned floats that correspond to a location? So far I have the following:

myVar = cur.execute("SELECT latitude, longitude FROM coords WHERE location ='" + jobLocation + "';")
if myVar == 1:
    print(cur.fetchone())
else:
    try:
        _place, (_lat, _lon) = geos.geocode(jobLocation, region='GB', exactly_one=False)
        print("%s: %.5f, %.5f" % _place, (_lat, _lon))
    except ValueError as err:
       print(err)

The code works (well, not really...) but I have no idea of how to get the returned coordinates into separate float variables.

Can you help?

share|improve this question

1 Answer 1

When you do cur.fetchone(), you need to store the result somewhere:

row = cur.fetchone()
print row[0], row[1]

Now row[0] will contain the latitude, and row[1] the longitude.

If you do this when connecting:

cur = con.cursor(mdb.cursors.DictCursor)

you can then use a dictionary to refer to the columns by name:

row = cur.fetchone()
print row["latitude"], row["longitude"]
share|improve this answer
    
Excellent. Thanks. –  uncle-junky Jun 14 '13 at 17:47
    
If this answer worked for you, please mark it as the accepted answer, so that other users know that it worked and that the question has been answered. Thanks. –  Duncan Lock Jun 14 '13 at 17:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.