Search code examples
pythonsqlsql-servert-sql

Retrieve data from sql server database using Python


I am trying to execute the following script. but I don't get neither the desired results nor a error message ,and I can't figure out where I'm doing wrong.

import pyodbc 
cnxn = pyodbc.connect("Driver={SQL Server Native Client 11.0};"
                        "Server=mySRVERNAME;"
                        "Database=MYDB;"
                        "uid=sa;pwd=MYPWD;"
                        "Trusted_Connection=yes;")


cursor = cnxn.cursor()
cursor.execute('select DISTINCT firstname,lastname,coalesce(middlename,\' \') as middlename from Person.Person')

for row in cursor:
    print('row = %r' % (row,))

any ideas ? any help is appreciated :)


Solution

  • You have to use a fetch method along with cursor. For Example

    for row in cursor.fetchall():
        print('row = %r' % (row,))
    

    EDIT :

    The fetchall function returns all remaining rows in a list.

        If there are no rows, an empty list is returned. 
        If there are a lot of rows, *this will use a lot of memory.* 
    

    Unread rows are stored by the database driver in a compact format and are often sent in batches from the database server.

    Reading in only the rows you need at one time will save a lot of memory.

    If we are going to process the rows one at a time, we can use the cursor itself as an interator Moreover we can simplify it since cursor.execute() always returns a cursor :

    for row in cursor.execute("select bla, anotherbla from blabla"): 
        print row.bla, row.anotherbla
    

    Documentation