double for with python

Asked

Viewed 709 times

0

Good evening.

Staff made the connection to the database and I need to compare two tables tab1 and tab2 so that the external "for" fixes the first line of "tab1" in the zero column and "for" internal traverse all rows of column one of tab2 so on until comparing the last row of tab1 with the last tab2 tried to mount here, but not this 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])

inserir a descrição da imagem aqui

2 answers

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]
    .... 
  • Ola Leandro, good afternoon. This code returns the error: Error: ('HY000', '[HY000] [Microsoft][ODBC SQL Server Driver]Busy connection with the results of another HSTMT (0) (Sqlexecdirectw)') - is passing two cursors on the same connection.

0

I believe that the ideial would be 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 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
  • Ola Leandro, good night. Thanks for the feedback your idea seems better than mine, but sorry ignorance is that I did not understand how I can sweep the tables and make the comparisons by assigning two cursors to "tab1" could you please explain to me how this works? I enclosed in the question two sample tables. Thank you

  • Opa, right you will compare the values of the two cursors tab1['nome_da_coluna'] will get the stored value of it, when it compares cursor1 == cursor2 the result will be different because we are not comparing data but two instances.

  • Sorry for the inconvenience, but I’m getting beat up here to understand some things for example - how should I declare the two cursors pointing to the same query? how should I store the contents of tab2 have to create a third cursor? Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.