Search code examples
pythonmysqlpymysql

Is there some way to save a mysql columns value in a python var?


I'm trying to save a column value into a python variable; I query my DB for a int in a columns called id_mType and id_meter depending of a value that I get from a XML. To test it I do the next (I'm new using databases):

m = 'R1'
id_cont1 = 'LGZ0019800712'
xdb = cursor.execute("SELECT id_mType FROM mType WHERE m_symbol = %s", m)
xdb1 = cursor.execute("select id_meter from meter where nombre = %s", 
id_cont1)

print (xdb)
print (xdb1)

I get every time the value "1" where the id_mType for 'R1' = 3 and id_meter= 7 for id_cont1 value. I need this to insert in another table (where there are both FK: id_meter and id_mType. Dont know if there is an easiest way)


Solution

  • You can store it in a list. Is that okay?

    results=cursor.fetchall()
    my_list=[]
    for result in results:
        my_list.append(result[0])
    

    Now my_list should hold the SQL column you get returned with your query.