-1

I am new to Python and can't seem to figure out why the .getRow method doesn't run. I created a DBMain class in dbMain.py and I am using pyTest.py to create the DBMain object to run getRow. When I run the debugger in Eclipse and DBMain's constructor does run but but when the getRow method is call nothing happens.

pyTest.py

import dbMain

def main():
    db = dbMain.DbMain()
    db.getRow()

if __name__ == '__main__':
    main()

dbMain.py

#@PydevCodeAnalysisIgnore
import pyodbc
class DbMain(object):
    cncx = ''
    def __init__(self):
        cnxn = pyodbc.connect(driver='{SQL Server}', 
                              server='server', 
                              database='database', 
                              uid='name', 
                              pwd='pwd')

    def getRow(): 
        cursor = cnxn.cursor()
        cursor.execute("select user_id, user_name from users")
        row = cursor.fetchone()
        return row
Chad
  • 273
  • 3
  • 8
  • 19

1 Answers1

1
  1. You do not return anything from getRow. Maybe you want to include something like

    ...
    return row
    
  2. Your getRow() method is not bound to the class. The signature for an instance method should look something like getRow(self) - the first parameter is the instance, which is received explicitly (but passed implicitly, when you call someinstance.method()).

To have something functional, you maybe should alter your dbMain to something like this:

#@PydevCodeAnalysisIgnore
import pyodbc
class DbMain(object):
    def __init__(self):
        # make cnxn an attribute of the instance
        self.cnxn = pyodbc.connect(driver='{SQL Server}', server='server', 
            database='database', uid='name', pwd='pwd')

    # receive `self` explicitly
    def getRow(self): 
        cursor = self.cnxn.cursor()
        cursor.execute("select user_id, user_name from users")
        row = cursor.fetchone()
        # actually return something
        return row

Further reading:

Community
  • 1
  • 1
miku
  • 181,842
  • 47
  • 306
  • 310