Why fetchall always results none?

60 views Asked by At

I just need help to solve this, when I execute my code, the result of the fetchall is 'None', but these are the expected results (https://i.stack.imgur.com/OZL5R.png)

this is my code

def verificar_saldo(ID_MATE_PERD):

    conn_str = f"DRIVER={driver};SERVER={server};DATABASE={database};UID={username};PWD={password}"
    conn = pyodbc.connect(conn_str)
    
   
    cursor = conn.cursor()
    query = f"EXEC PRC_COME_ESTO_DISP @ID_MATE = {ID_MATE_PERD}"

    cursor.execute(sql + query)

    resultados = cursor.fetchall()
    print(resultados)  # Imprimir os resultados para depuração

    cursor.close()
    conn.close()

    return resultados
1

There are 1 answers

1
Nate Millner On

Few problems with your code, For one you are subject to SQL injection by using formatted strings. Next, it looks like you have a variable called "SQL" in your execution function, I belive this is unneeded and you should just be calling your query string.

def verificar_saldo(ID_MATE_PERD):

    conn_str = f"DRIVER={driver};SERVER={server};DATABASE={database};UID={username};PWD={password}"
    conn = pyodbc.connect(conn_str)
    
   
    cursor = conn.cursor()
    query = "EXEC PRC_COME_ESTO_DISP @ID_MATE = %s"%(ID_MATE_PERD)

    cursor.execute(query)

    resultados = cursor.fetchall()
    print(resultados)  # Imprimir os resultados para depuração

    conn.close()

    return resultados

Try something like this. I am not familiar with your SQL package so this could need some tweaking.