double for with python

0

Expensive, good night.

Personal I made the connection to the database and I need to compare two tables tab1 and tab2 so that the external "for" fix the first line of "tab1" in column zero and the "for" internal scrolls all the lines of the column one of tab2 so on until comparing the last row of tab1 with the last tab2 I tried to mount here but not correct could anyone help? Thank you.

import pyodbc
conn = pyodbc.connect("DRIVER={SQL Server};Server=localhost;database=teste;uid=;pwd=")
cursor = conn.cursor()
cursor.execute('select * from tab1')
cursor.execute('select * from tab2')

for row in cursor.fetchall():
    print (row[0])
    for row in cursor.fetchall():
        print (row[1])

    
asked by anonymous 28.04.2018 / 05:13

2 answers

0

I believe the ideial would be for you to filter what you are already looking for in your query, and avoid processing on the application side. Making a simple comparison later. If you use the same cursor that you used to store the query the first time you will lose the value returned by the database as well. Create two variables.

# SELECT TOP 1 * FROM TAB1 ORDER BY ID DESC

tab1 = cursor1.fetchone()
tab1 = cursor2.fetchone()
if tab1['field'] == tab2['field']:
    # definir aqui a lógica
    
28.04.2018 / 10:30
0
class Capacity:
    def __init__(self, cod_sala, capacidade, idade):
        self.cod_sale = cod_sala
        self.capacidade = capacidade
        self.idade = idade
        self.students = []



import pyodbc
import re
conn = pyodbc.connect("DRIVER={SQL Server};Server=localhost;database=teste;uid=;pwd=")

cursor1 = conn.cursor()
cursor2 = conn.cursor()

cursor1.execute('select * from tab1')
cursor2.execute('select * from tab2')

class_capacity = []

for row in cursor1.fetchall():
    class_capacity.append(Capacity(row['cod_sale'], row['capacidade'], row['idade']))

for row2 in cursor2.fetchall():
    for cc in class_capacity:
        extract = cc.idade.split('1')
        value = '1' + extract[1]
        condition = extract[0]
    .... 
    
29.04.2018 / 15:30