How to return a list from SQL query using pyodbc?












4














I am trying to run a select query to retrieve data from SQL Server using pyodbc in python 2.7. I want the data to be returned in a list. The code I have written is below.



It works, kinda, but not in the way I expected. My returned list looks something like below:



Index     Type     Size        Value
0 Row 1 Row object of pyodbc module
1 Row 1 Row object of pyodbc module
...
105 Row 1 Row object of pyodbc module


I was hoping to see something like below (i.e. my table in SQL)



ActionId   AnnDate      Name    SaleValue
128929 2018-01-01 Bob 105.3
193329 2018-04-05 Bob 1006.98
...
23654 2018-11-21 Bob 103.32


Is a list not the best way to return data from a SQL query using pyodbc?



Code



import pyodbc


def GetSQLData(dbName, query):

sPass = 'MyPassword'
sServer = 'MyServer\SQL1'
uname = 'MyUser'

cnxn = pyodbc.connect("Driver={SQL Server Native Client 11.0};"
"Server=" + sServer + ";"
"Database=" + dbName + ";"
"uid=" + uname + ";pwd=" + sPass)

cursor = cnxn.cursor()
cursor.execute(query)

return list(cursor.fetchall())









share|improve this question




















  • 1




    What is the query you're running?
    – Edgar R. Mondragón
    Oct 31 '18 at 15:12










  • just a select query, very simple for testing purpose. so about 10 columns, dates, intergers, text
    – mHelpMe
    Oct 31 '18 at 15:27






  • 1




    If you look at the documentation about pyodbc cursor objects, you'll find that each row has a set of attributes that correspond in name to the column names from your table. While I have no database to test, I have a slight feeling that pyodbc doesn't actually follow the Python DB API 2.0 fully, and that a single row is not a list of its columns.
    – 9769953
    Nov 5 '18 at 9:06










  • Why do you want to return the data as a list? Data from SQL Server are in table format. The analogue of that in Python is a Pandas DataFrame and the easiest way to get that is via pd.read_sql with a pyodbc connection. See m33n's answer below, in my estimation that is the best solution to your problem
    – Karl
    Nov 9 '18 at 7:19


















4














I am trying to run a select query to retrieve data from SQL Server using pyodbc in python 2.7. I want the data to be returned in a list. The code I have written is below.



It works, kinda, but not in the way I expected. My returned list looks something like below:



Index     Type     Size        Value
0 Row 1 Row object of pyodbc module
1 Row 1 Row object of pyodbc module
...
105 Row 1 Row object of pyodbc module


I was hoping to see something like below (i.e. my table in SQL)



ActionId   AnnDate      Name    SaleValue
128929 2018-01-01 Bob 105.3
193329 2018-04-05 Bob 1006.98
...
23654 2018-11-21 Bob 103.32


Is a list not the best way to return data from a SQL query using pyodbc?



Code



import pyodbc


def GetSQLData(dbName, query):

sPass = 'MyPassword'
sServer = 'MyServer\SQL1'
uname = 'MyUser'

cnxn = pyodbc.connect("Driver={SQL Server Native Client 11.0};"
"Server=" + sServer + ";"
"Database=" + dbName + ";"
"uid=" + uname + ";pwd=" + sPass)

cursor = cnxn.cursor()
cursor.execute(query)

return list(cursor.fetchall())









share|improve this question




















  • 1




    What is the query you're running?
    – Edgar R. Mondragón
    Oct 31 '18 at 15:12










  • just a select query, very simple for testing purpose. so about 10 columns, dates, intergers, text
    – mHelpMe
    Oct 31 '18 at 15:27






  • 1




    If you look at the documentation about pyodbc cursor objects, you'll find that each row has a set of attributes that correspond in name to the column names from your table. While I have no database to test, I have a slight feeling that pyodbc doesn't actually follow the Python DB API 2.0 fully, and that a single row is not a list of its columns.
    – 9769953
    Nov 5 '18 at 9:06










  • Why do you want to return the data as a list? Data from SQL Server are in table format. The analogue of that in Python is a Pandas DataFrame and the easiest way to get that is via pd.read_sql with a pyodbc connection. See m33n's answer below, in my estimation that is the best solution to your problem
    – Karl
    Nov 9 '18 at 7:19
















4












4








4


2





I am trying to run a select query to retrieve data from SQL Server using pyodbc in python 2.7. I want the data to be returned in a list. The code I have written is below.



It works, kinda, but not in the way I expected. My returned list looks something like below:



Index     Type     Size        Value
0 Row 1 Row object of pyodbc module
1 Row 1 Row object of pyodbc module
...
105 Row 1 Row object of pyodbc module


I was hoping to see something like below (i.e. my table in SQL)



ActionId   AnnDate      Name    SaleValue
128929 2018-01-01 Bob 105.3
193329 2018-04-05 Bob 1006.98
...
23654 2018-11-21 Bob 103.32


Is a list not the best way to return data from a SQL query using pyodbc?



Code



import pyodbc


def GetSQLData(dbName, query):

sPass = 'MyPassword'
sServer = 'MyServer\SQL1'
uname = 'MyUser'

cnxn = pyodbc.connect("Driver={SQL Server Native Client 11.0};"
"Server=" + sServer + ";"
"Database=" + dbName + ";"
"uid=" + uname + ";pwd=" + sPass)

cursor = cnxn.cursor()
cursor.execute(query)

return list(cursor.fetchall())









share|improve this question















I am trying to run a select query to retrieve data from SQL Server using pyodbc in python 2.7. I want the data to be returned in a list. The code I have written is below.



It works, kinda, but not in the way I expected. My returned list looks something like below:



Index     Type     Size        Value
0 Row 1 Row object of pyodbc module
1 Row 1 Row object of pyodbc module
...
105 Row 1 Row object of pyodbc module


I was hoping to see something like below (i.e. my table in SQL)



ActionId   AnnDate      Name    SaleValue
128929 2018-01-01 Bob 105.3
193329 2018-04-05 Bob 1006.98
...
23654 2018-11-21 Bob 103.32


Is a list not the best way to return data from a SQL query using pyodbc?



Code



import pyodbc


def GetSQLData(dbName, query):

sPass = 'MyPassword'
sServer = 'MyServer\SQL1'
uname = 'MyUser'

cnxn = pyodbc.connect("Driver={SQL Server Native Client 11.0};"
"Server=" + sServer + ";"
"Database=" + dbName + ";"
"uid=" + uname + ";pwd=" + sPass)

cursor = cnxn.cursor()
cursor.execute(query)

return list(cursor.fetchall())






python python-2.7 list pyodbc






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 '18 at 18:04









benvc

4,1171319




4,1171319










asked Oct 31 '18 at 14:22









mHelpMemHelpMe

1,965144076




1,965144076








  • 1




    What is the query you're running?
    – Edgar R. Mondragón
    Oct 31 '18 at 15:12










  • just a select query, very simple for testing purpose. so about 10 columns, dates, intergers, text
    – mHelpMe
    Oct 31 '18 at 15:27






  • 1




    If you look at the documentation about pyodbc cursor objects, you'll find that each row has a set of attributes that correspond in name to the column names from your table. While I have no database to test, I have a slight feeling that pyodbc doesn't actually follow the Python DB API 2.0 fully, and that a single row is not a list of its columns.
    – 9769953
    Nov 5 '18 at 9:06










  • Why do you want to return the data as a list? Data from SQL Server are in table format. The analogue of that in Python is a Pandas DataFrame and the easiest way to get that is via pd.read_sql with a pyodbc connection. See m33n's answer below, in my estimation that is the best solution to your problem
    – Karl
    Nov 9 '18 at 7:19
















  • 1




    What is the query you're running?
    – Edgar R. Mondragón
    Oct 31 '18 at 15:12










  • just a select query, very simple for testing purpose. so about 10 columns, dates, intergers, text
    – mHelpMe
    Oct 31 '18 at 15:27






  • 1




    If you look at the documentation about pyodbc cursor objects, you'll find that each row has a set of attributes that correspond in name to the column names from your table. While I have no database to test, I have a slight feeling that pyodbc doesn't actually follow the Python DB API 2.0 fully, and that a single row is not a list of its columns.
    – 9769953
    Nov 5 '18 at 9:06










  • Why do you want to return the data as a list? Data from SQL Server are in table format. The analogue of that in Python is a Pandas DataFrame and the easiest way to get that is via pd.read_sql with a pyodbc connection. See m33n's answer below, in my estimation that is the best solution to your problem
    – Karl
    Nov 9 '18 at 7:19










1




1




What is the query you're running?
– Edgar R. Mondragón
Oct 31 '18 at 15:12




What is the query you're running?
– Edgar R. Mondragón
Oct 31 '18 at 15:12












just a select query, very simple for testing purpose. so about 10 columns, dates, intergers, text
– mHelpMe
Oct 31 '18 at 15:27




just a select query, very simple for testing purpose. so about 10 columns, dates, intergers, text
– mHelpMe
Oct 31 '18 at 15:27




1




1




If you look at the documentation about pyodbc cursor objects, you'll find that each row has a set of attributes that correspond in name to the column names from your table. While I have no database to test, I have a slight feeling that pyodbc doesn't actually follow the Python DB API 2.0 fully, and that a single row is not a list of its columns.
– 9769953
Nov 5 '18 at 9:06




If you look at the documentation about pyodbc cursor objects, you'll find that each row has a set of attributes that correspond in name to the column names from your table. While I have no database to test, I have a slight feeling that pyodbc doesn't actually follow the Python DB API 2.0 fully, and that a single row is not a list of its columns.
– 9769953
Nov 5 '18 at 9:06












Why do you want to return the data as a list? Data from SQL Server are in table format. The analogue of that in Python is a Pandas DataFrame and the easiest way to get that is via pd.read_sql with a pyodbc connection. See m33n's answer below, in my estimation that is the best solution to your problem
– Karl
Nov 9 '18 at 7:19






Why do you want to return the data as a list? Data from SQL Server are in table format. The analogue of that in Python is a Pandas DataFrame and the easiest way to get that is via pd.read_sql with a pyodbc connection. See m33n's answer below, in my estimation that is the best solution to your problem
– Karl
Nov 9 '18 at 7:19














2 Answers
2






active

oldest

votes


















1














If you want to return your query results as a list of lists with your column names as the first sublist (similar to the example output in your question), then you can do something like the following:



import pyodbc


cnxn = pyodbc.connect("YOUR_CONNECTION_STRING")
cursor = cnxn.cursor()

cursor.execute("YOUR_QUERY")

columns = [column[0] for column in cursor.description]
results = [columns] + [row for row in cursor.fetchall()]

for result in results:
print result

# EXAMPLE OUTPUT
# ['col1', 'col2']
# ['r1c1', 'r1c2']
# ['r2c1', 'r2c2']


Depending on how you are using the results, I often find it more useful to a have a list of dicts. For example:



results = [dict(zip(columns, row)) for row in cursor.fetchall()]

for result in results:
print result

# EXAMPLE OUTPUT
# {'col1': 'r1c1', 'col2':'r1c2'}
# {'col1': 'r2c1', 'col2':'r2c2'}





share|improve this answer































    1














    There is even a better option than a list, try Pandas DataFrame!
    It helps to deal with column names and apply column wise operations!



    import pandas as pd
    import pyodbc


    def GetSQLData(dbName, query):

    sPass = 'MyPassword'
    sServer = 'MyServer\SQL1'
    uname = 'MyUser'

    cnxn = pyodbc.connect("Driver={SQL Server Native Client 11.0};"
    "Server=" + sServer + ";"
    "Database=" + dbName + ";"
    "uid=" + uname + ";pwd=" + sPass)


    df = pd.read_sql(cnxn, query)

    return df # Pandas Dataframe


    EDIT:



    If you prefer a list of lists, (this means one list per row) you can obtain it by:



    df.values.tolist()  # list of lists 


    But I highly recommend you to start working with pandas






    share|improve this answer























    • I don't think this helps: my guess is that Pandas does something very similar to list(cursor.fetchall()) under the hood, and you'd be stuck with a dataframe similar to the output obtained in the question.
      – 9769953
      Nov 5 '18 at 9:07










    • Why would you prefer a list instead of a dataframe? A list is a simpler data structure but you will need to know the ordering of your columns in you query for example. With pandas you can deal with the column name directly! Also it has a large number of operations that you can apply to your data after queried that can be executed in parallel with very simple code. Pandas is one of the main python libraries nowadays :)
      – m33n
      Nov 5 '18 at 9:10










    • That's not my point: the dataframe itself will likely contain columns such as given in the question: "0, Row, 1, Row object of pyodbc module", not "128929, 2018-01-01, Bob, 105.3" as preferred. A dataframe would be equally unusable as a (double) list.
      – 9769953
      Nov 5 '18 at 10:08








    • 1




      @9769953 - "the dataframe itself will likely contain columns such as given in the question: '0, Row, 1, Row object of pyodbc module'". No, it won't. pandas will unpack the pyodbc.Row objects into a proper DataFrame. Try it and see.
      – Gord Thompson
      Nov 5 '18 at 17:16












    • Thanks @GordThompson, I got two negative votes for a valid solution to this problem...
      – m33n
      Nov 6 '18 at 8:12











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53085607%2fhow-to-return-a-list-from-sql-query-using-pyodbc%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    If you want to return your query results as a list of lists with your column names as the first sublist (similar to the example output in your question), then you can do something like the following:



    import pyodbc


    cnxn = pyodbc.connect("YOUR_CONNECTION_STRING")
    cursor = cnxn.cursor()

    cursor.execute("YOUR_QUERY")

    columns = [column[0] for column in cursor.description]
    results = [columns] + [row for row in cursor.fetchall()]

    for result in results:
    print result

    # EXAMPLE OUTPUT
    # ['col1', 'col2']
    # ['r1c1', 'r1c2']
    # ['r2c1', 'r2c2']


    Depending on how you are using the results, I often find it more useful to a have a list of dicts. For example:



    results = [dict(zip(columns, row)) for row in cursor.fetchall()]

    for result in results:
    print result

    # EXAMPLE OUTPUT
    # {'col1': 'r1c1', 'col2':'r1c2'}
    # {'col1': 'r2c1', 'col2':'r2c2'}





    share|improve this answer




























      1














      If you want to return your query results as a list of lists with your column names as the first sublist (similar to the example output in your question), then you can do something like the following:



      import pyodbc


      cnxn = pyodbc.connect("YOUR_CONNECTION_STRING")
      cursor = cnxn.cursor()

      cursor.execute("YOUR_QUERY")

      columns = [column[0] for column in cursor.description]
      results = [columns] + [row for row in cursor.fetchall()]

      for result in results:
      print result

      # EXAMPLE OUTPUT
      # ['col1', 'col2']
      # ['r1c1', 'r1c2']
      # ['r2c1', 'r2c2']


      Depending on how you are using the results, I often find it more useful to a have a list of dicts. For example:



      results = [dict(zip(columns, row)) for row in cursor.fetchall()]

      for result in results:
      print result

      # EXAMPLE OUTPUT
      # {'col1': 'r1c1', 'col2':'r1c2'}
      # {'col1': 'r2c1', 'col2':'r2c2'}





      share|improve this answer


























        1












        1








        1






        If you want to return your query results as a list of lists with your column names as the first sublist (similar to the example output in your question), then you can do something like the following:



        import pyodbc


        cnxn = pyodbc.connect("YOUR_CONNECTION_STRING")
        cursor = cnxn.cursor()

        cursor.execute("YOUR_QUERY")

        columns = [column[0] for column in cursor.description]
        results = [columns] + [row for row in cursor.fetchall()]

        for result in results:
        print result

        # EXAMPLE OUTPUT
        # ['col1', 'col2']
        # ['r1c1', 'r1c2']
        # ['r2c1', 'r2c2']


        Depending on how you are using the results, I often find it more useful to a have a list of dicts. For example:



        results = [dict(zip(columns, row)) for row in cursor.fetchall()]

        for result in results:
        print result

        # EXAMPLE OUTPUT
        # {'col1': 'r1c1', 'col2':'r1c2'}
        # {'col1': 'r2c1', 'col2':'r2c2'}





        share|improve this answer














        If you want to return your query results as a list of lists with your column names as the first sublist (similar to the example output in your question), then you can do something like the following:



        import pyodbc


        cnxn = pyodbc.connect("YOUR_CONNECTION_STRING")
        cursor = cnxn.cursor()

        cursor.execute("YOUR_QUERY")

        columns = [column[0] for column in cursor.description]
        results = [columns] + [row for row in cursor.fetchall()]

        for result in results:
        print result

        # EXAMPLE OUTPUT
        # ['col1', 'col2']
        # ['r1c1', 'r1c2']
        # ['r2c1', 'r2c2']


        Depending on how you are using the results, I often find it more useful to a have a list of dicts. For example:



        results = [dict(zip(columns, row)) for row in cursor.fetchall()]

        for result in results:
        print result

        # EXAMPLE OUTPUT
        # {'col1': 'r1c1', 'col2':'r1c2'}
        # {'col1': 'r2c1', 'col2':'r2c2'}






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 9 '18 at 18:06

























        answered Nov 9 '18 at 17:56









        benvcbenvc

        4,1171319




        4,1171319

























            1














            There is even a better option than a list, try Pandas DataFrame!
            It helps to deal with column names and apply column wise operations!



            import pandas as pd
            import pyodbc


            def GetSQLData(dbName, query):

            sPass = 'MyPassword'
            sServer = 'MyServer\SQL1'
            uname = 'MyUser'

            cnxn = pyodbc.connect("Driver={SQL Server Native Client 11.0};"
            "Server=" + sServer + ";"
            "Database=" + dbName + ";"
            "uid=" + uname + ";pwd=" + sPass)


            df = pd.read_sql(cnxn, query)

            return df # Pandas Dataframe


            EDIT:



            If you prefer a list of lists, (this means one list per row) you can obtain it by:



            df.values.tolist()  # list of lists 


            But I highly recommend you to start working with pandas






            share|improve this answer























            • I don't think this helps: my guess is that Pandas does something very similar to list(cursor.fetchall()) under the hood, and you'd be stuck with a dataframe similar to the output obtained in the question.
              – 9769953
              Nov 5 '18 at 9:07










            • Why would you prefer a list instead of a dataframe? A list is a simpler data structure but you will need to know the ordering of your columns in you query for example. With pandas you can deal with the column name directly! Also it has a large number of operations that you can apply to your data after queried that can be executed in parallel with very simple code. Pandas is one of the main python libraries nowadays :)
              – m33n
              Nov 5 '18 at 9:10










            • That's not my point: the dataframe itself will likely contain columns such as given in the question: "0, Row, 1, Row object of pyodbc module", not "128929, 2018-01-01, Bob, 105.3" as preferred. A dataframe would be equally unusable as a (double) list.
              – 9769953
              Nov 5 '18 at 10:08








            • 1




              @9769953 - "the dataframe itself will likely contain columns such as given in the question: '0, Row, 1, Row object of pyodbc module'". No, it won't. pandas will unpack the pyodbc.Row objects into a proper DataFrame. Try it and see.
              – Gord Thompson
              Nov 5 '18 at 17:16












            • Thanks @GordThompson, I got two negative votes for a valid solution to this problem...
              – m33n
              Nov 6 '18 at 8:12
















            1














            There is even a better option than a list, try Pandas DataFrame!
            It helps to deal with column names and apply column wise operations!



            import pandas as pd
            import pyodbc


            def GetSQLData(dbName, query):

            sPass = 'MyPassword'
            sServer = 'MyServer\SQL1'
            uname = 'MyUser'

            cnxn = pyodbc.connect("Driver={SQL Server Native Client 11.0};"
            "Server=" + sServer + ";"
            "Database=" + dbName + ";"
            "uid=" + uname + ";pwd=" + sPass)


            df = pd.read_sql(cnxn, query)

            return df # Pandas Dataframe


            EDIT:



            If you prefer a list of lists, (this means one list per row) you can obtain it by:



            df.values.tolist()  # list of lists 


            But I highly recommend you to start working with pandas






            share|improve this answer























            • I don't think this helps: my guess is that Pandas does something very similar to list(cursor.fetchall()) under the hood, and you'd be stuck with a dataframe similar to the output obtained in the question.
              – 9769953
              Nov 5 '18 at 9:07










            • Why would you prefer a list instead of a dataframe? A list is a simpler data structure but you will need to know the ordering of your columns in you query for example. With pandas you can deal with the column name directly! Also it has a large number of operations that you can apply to your data after queried that can be executed in parallel with very simple code. Pandas is one of the main python libraries nowadays :)
              – m33n
              Nov 5 '18 at 9:10










            • That's not my point: the dataframe itself will likely contain columns such as given in the question: "0, Row, 1, Row object of pyodbc module", not "128929, 2018-01-01, Bob, 105.3" as preferred. A dataframe would be equally unusable as a (double) list.
              – 9769953
              Nov 5 '18 at 10:08








            • 1




              @9769953 - "the dataframe itself will likely contain columns such as given in the question: '0, Row, 1, Row object of pyodbc module'". No, it won't. pandas will unpack the pyodbc.Row objects into a proper DataFrame. Try it and see.
              – Gord Thompson
              Nov 5 '18 at 17:16












            • Thanks @GordThompson, I got two negative votes for a valid solution to this problem...
              – m33n
              Nov 6 '18 at 8:12














            1












            1








            1






            There is even a better option than a list, try Pandas DataFrame!
            It helps to deal with column names and apply column wise operations!



            import pandas as pd
            import pyodbc


            def GetSQLData(dbName, query):

            sPass = 'MyPassword'
            sServer = 'MyServer\SQL1'
            uname = 'MyUser'

            cnxn = pyodbc.connect("Driver={SQL Server Native Client 11.0};"
            "Server=" + sServer + ";"
            "Database=" + dbName + ";"
            "uid=" + uname + ";pwd=" + sPass)


            df = pd.read_sql(cnxn, query)

            return df # Pandas Dataframe


            EDIT:



            If you prefer a list of lists, (this means one list per row) you can obtain it by:



            df.values.tolist()  # list of lists 


            But I highly recommend you to start working with pandas






            share|improve this answer














            There is even a better option than a list, try Pandas DataFrame!
            It helps to deal with column names and apply column wise operations!



            import pandas as pd
            import pyodbc


            def GetSQLData(dbName, query):

            sPass = 'MyPassword'
            sServer = 'MyServer\SQL1'
            uname = 'MyUser'

            cnxn = pyodbc.connect("Driver={SQL Server Native Client 11.0};"
            "Server=" + sServer + ";"
            "Database=" + dbName + ";"
            "uid=" + uname + ";pwd=" + sPass)


            df = pd.read_sql(cnxn, query)

            return df # Pandas Dataframe


            EDIT:



            If you prefer a list of lists, (this means one list per row) you can obtain it by:



            df.values.tolist()  # list of lists 


            But I highly recommend you to start working with pandas







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 13 '18 at 8:34

























            answered Nov 5 '18 at 8:57









            m33nm33n

            629218




            629218












            • I don't think this helps: my guess is that Pandas does something very similar to list(cursor.fetchall()) under the hood, and you'd be stuck with a dataframe similar to the output obtained in the question.
              – 9769953
              Nov 5 '18 at 9:07










            • Why would you prefer a list instead of a dataframe? A list is a simpler data structure but you will need to know the ordering of your columns in you query for example. With pandas you can deal with the column name directly! Also it has a large number of operations that you can apply to your data after queried that can be executed in parallel with very simple code. Pandas is one of the main python libraries nowadays :)
              – m33n
              Nov 5 '18 at 9:10










            • That's not my point: the dataframe itself will likely contain columns such as given in the question: "0, Row, 1, Row object of pyodbc module", not "128929, 2018-01-01, Bob, 105.3" as preferred. A dataframe would be equally unusable as a (double) list.
              – 9769953
              Nov 5 '18 at 10:08








            • 1




              @9769953 - "the dataframe itself will likely contain columns such as given in the question: '0, Row, 1, Row object of pyodbc module'". No, it won't. pandas will unpack the pyodbc.Row objects into a proper DataFrame. Try it and see.
              – Gord Thompson
              Nov 5 '18 at 17:16












            • Thanks @GordThompson, I got two negative votes for a valid solution to this problem...
              – m33n
              Nov 6 '18 at 8:12


















            • I don't think this helps: my guess is that Pandas does something very similar to list(cursor.fetchall()) under the hood, and you'd be stuck with a dataframe similar to the output obtained in the question.
              – 9769953
              Nov 5 '18 at 9:07










            • Why would you prefer a list instead of a dataframe? A list is a simpler data structure but you will need to know the ordering of your columns in you query for example. With pandas you can deal with the column name directly! Also it has a large number of operations that you can apply to your data after queried that can be executed in parallel with very simple code. Pandas is one of the main python libraries nowadays :)
              – m33n
              Nov 5 '18 at 9:10










            • That's not my point: the dataframe itself will likely contain columns such as given in the question: "0, Row, 1, Row object of pyodbc module", not "128929, 2018-01-01, Bob, 105.3" as preferred. A dataframe would be equally unusable as a (double) list.
              – 9769953
              Nov 5 '18 at 10:08








            • 1




              @9769953 - "the dataframe itself will likely contain columns such as given in the question: '0, Row, 1, Row object of pyodbc module'". No, it won't. pandas will unpack the pyodbc.Row objects into a proper DataFrame. Try it and see.
              – Gord Thompson
              Nov 5 '18 at 17:16












            • Thanks @GordThompson, I got two negative votes for a valid solution to this problem...
              – m33n
              Nov 6 '18 at 8:12
















            I don't think this helps: my guess is that Pandas does something very similar to list(cursor.fetchall()) under the hood, and you'd be stuck with a dataframe similar to the output obtained in the question.
            – 9769953
            Nov 5 '18 at 9:07




            I don't think this helps: my guess is that Pandas does something very similar to list(cursor.fetchall()) under the hood, and you'd be stuck with a dataframe similar to the output obtained in the question.
            – 9769953
            Nov 5 '18 at 9:07












            Why would you prefer a list instead of a dataframe? A list is a simpler data structure but you will need to know the ordering of your columns in you query for example. With pandas you can deal with the column name directly! Also it has a large number of operations that you can apply to your data after queried that can be executed in parallel with very simple code. Pandas is one of the main python libraries nowadays :)
            – m33n
            Nov 5 '18 at 9:10




            Why would you prefer a list instead of a dataframe? A list is a simpler data structure but you will need to know the ordering of your columns in you query for example. With pandas you can deal with the column name directly! Also it has a large number of operations that you can apply to your data after queried that can be executed in parallel with very simple code. Pandas is one of the main python libraries nowadays :)
            – m33n
            Nov 5 '18 at 9:10












            That's not my point: the dataframe itself will likely contain columns such as given in the question: "0, Row, 1, Row object of pyodbc module", not "128929, 2018-01-01, Bob, 105.3" as preferred. A dataframe would be equally unusable as a (double) list.
            – 9769953
            Nov 5 '18 at 10:08






            That's not my point: the dataframe itself will likely contain columns such as given in the question: "0, Row, 1, Row object of pyodbc module", not "128929, 2018-01-01, Bob, 105.3" as preferred. A dataframe would be equally unusable as a (double) list.
            – 9769953
            Nov 5 '18 at 10:08






            1




            1




            @9769953 - "the dataframe itself will likely contain columns such as given in the question: '0, Row, 1, Row object of pyodbc module'". No, it won't. pandas will unpack the pyodbc.Row objects into a proper DataFrame. Try it and see.
            – Gord Thompson
            Nov 5 '18 at 17:16






            @9769953 - "the dataframe itself will likely contain columns such as given in the question: '0, Row, 1, Row object of pyodbc module'". No, it won't. pandas will unpack the pyodbc.Row objects into a proper DataFrame. Try it and see.
            – Gord Thompson
            Nov 5 '18 at 17:16














            Thanks @GordThompson, I got two negative votes for a valid solution to this problem...
            – m33n
            Nov 6 '18 at 8:12




            Thanks @GordThompson, I got two negative votes for a valid solution to this problem...
            – m33n
            Nov 6 '18 at 8:12


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53085607%2fhow-to-return-a-list-from-sql-query-using-pyodbc%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            List item for chat from Array inside array React Native

            Jo Brand

            Thiostrepton