How to operate data in pyhon and sql server

0

My question may be quite simple. I need to make a sum of a column from my database. The problem is that (the way I do it), it brings me the data as rows and not as int. How do I get a data from the database as it is? Example:

def prueba():
    consulta= ("SELECT x from y")
    cur.execute(consulta)
    registros=cur.fetchall()
    for i in registros:
        print i

I understand that fetchall shows me the data as rows, but in what way do I bring it as an integer to be able to operate it? I hope you have given me to understand, thank you very much.

    
asked by andeehr 21.06.2018 в 01:54
source

1 answer

0

counter3 = resultconsolidation.fetchall ()

print (counter3) ## = > print the result of the query and you'll see the> & nt; pocicion of your data

for row_number, row_data in enumerate (counter3): here you go through the list

  
    
      

data1 = int (row_data [0])

             

data2 = int (row_data [1])

             

data3 = int (row_data [2]) already stored in variables you can operate with them

    
  
    
answered by 21.06.2018 в 08:08