Typeerror: fetchall() takes in Arguments (1 Given)

Asked

Viewed 118 times

0

When running the code below I get the title error, I tried to use a different code structure by assigning the results of select to a ma variable, tried tbm just bring a bank line with fetchone() and unfortunately I haven’t succeeded in trying.

import pyodbc import pymysql import the

Important class Bank: def init(self): self.server = 'localhost' self.database = 'lc_firmas' self.username = 'root' self.password = 'little girl' self.port = '3306' self.string_connection = pyodbc.connect( 'DRIVER={Devart ODBC Driver for Mysql};User ID='+self.username+';Password='+self.password+';Server='+self.server+';Database='+self.database+';Port='+self.port+';String Types=Unicode') self.connection = self.string_connection.cursor()

def gravarselos_aut(self):
    self.query = 'SELECT data, letra, NumIni, Numfin FROM selosaut'
    self.conexao.execute(self.query)
    resultado = conexao.fetchall()
    for linha in resultado:
        self.data = str(self.conexao.fetchall([0]))
        self.letra = str(self.conexao.fetchall([1]))
        self.Num_Inicial = str(self.conexao.fetchall([2]))
        self.Num_Fin = str(self.conexao.fetchall([3]))
        print(
            f'{linha} {self.data}, {self.letra}, {self.Num_Inicial}, {self.Num_Fin}')
        
  • 1

    The mistake says fetchall does not take arguments. In your code you are passing an argument. You have tried to change this self.conexao.fetchall([0]) for linha[0]?

1 answer

1

What happens is that the fecthall has already transformed the result of the consultation into a array and now with the for you only need to extract this values that were stored within resultado.

As a test, run a print only in the variable linha, that will become clearer:

for linha in resultado:
  print(linha)

output:
[valorData, valorLetra, valorNumIni, valorNumFin]
[valorData, valorLetra, valorNumIni, valorNumFin] ... 

Solution to your case:

def gravarselos_aut(self):
    self.query = 'SELECT data, letra, NumIni, Numfin FROM selosaut'
    self.conexao.execute(self.query)
    resultado = conexao.fetchall()
    for linha in resultado:
        self.data = str(linha[0])
        self.letra = str(linha[1])
        self.Num_Inicial = str(linha[2])
        self.Num_Fin = str(linha[3])
        print(f'{self.data}, {self.letra}, {self.Num_Inicial}, {self.Num_Fin}')

As a hint, I recommend that you use the mysql cursor as Dictionary,: mysql_cnx.cursor(buffered=True, dictionary=True)

For today to get a value, you need to inform the position of the value within the line, example linha[1]. But if by chance you include 4 more columns before the column Letra? What was linha[1] becomes linha[4], linha[2] becomes linha[5]... Imagine the inconvenience for the review?!

Now with the parameter dictionary no, because you will inform only the column name, see below:

def gravarselos_aut(self):
    self.query = 'SELECT data, letra, NumIni, Numfin FROM selosaut'
    self.conexao.execute(self.query)
    resultado = conexao.fetchall()
    for linha in resultado:
        self.data = str(linha["data"])
        self.letra = str(linha["letra"])
        self.Num_Inicial = str(linha["NumIni"])
        self.Num_Fin = str(linha["Numfin"])
        print(f'{self.data}, {self.letra}, {self.Num_Inicial}, {self.Num_Fin}')

Browser other questions tagged

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