[Python] How to convert SQL query results into a python dictionary

Discussão em 'Python' iniciado por Stack, Fevereiro 6, 2025.

  1. Stack

    Stack Membro Participativo

    Im having difficuty converting my results from a query into a python dictionary. Each dictionary is supposed to represent one student, the keys are the column name and the values are the corresponding values from the query, so far this is what I've come up with:

    def all_students():
    qu= 'select * from students'
    crs.execute(qu)
    for row in crs:
    student= {"StudentNum":row[0], "StudentLastName":row[2], "StudentFirst Name":row[3}
    return student


    But when i print it , it returns in correct information and everything is out of order, and it only displays one record :

    {'StudentLastName': Jane, StudentNum: 'Smith ', StudentFirst Name: '1612'}

    Continue reading...

Compartilhe esta Página