Two select query at a time python
Question: I want to calculate distance between two points. For that for each point in
one table i have to calculate distance with all the other point in another
table in same database. I am using python for that but I am not able to
execute two query at a time.

    
    
    import mysql.connector
    from haversine import haversine
    
    cnx = mysql.connector.connect(host='localhost',user='xxxxx',passwd='xxxxxx',db='xxxxxx')
    cursor = cnx.cursor()
    cursor2 = cnx.cursor()
    
    query = ("select longitude,latitude from roadData limit 5")
    cursor.execute(query)
    
    query2=("SELECT geo_coordinates_latitude, geo_coordinates_longitude from tweetmelbourne limit 2")
    cursor2.execute(query2)
    
    for (longitude,latitude) in cursor:
        print longitude
        print latitude
        for (geo_coordinates_longitude,geo_coordinates_latitude) in cursor2:
            print geo_coordinates_longitude
            print geo_coordinates_latitude
    
    
    cursor.close()
    cnx.close() 
    

But, I am getting error for second query execution

> InternalError: Unread result found.

I tried buffer=true for cursor but still same error. And also is it efficient
to do like this or any other better way.


Answer: One trick you could use here would be to cross join the two tables together:

    
    
    SELECT t1.longitude,
           t1.latitude,
           t2.geo_coordinates_latitude,
           t2.geo_coordinates_longitude
    FROM
    (
        SELECT longitude, latitude
        FROM roadData
        LIMIT 5
    ) t1
    CROSS JOIN
    (
        SELECT geo_coordinates_latitude, geo_coordinates_longitude
        FROM tweetmelbourne
        LIMIT 2
    ) t2
    

This should work because the way you iterate over the result set in your
Python code resembles a cross join. In this case, using the above query you
would only need to iterate once over the entire result set.

