2012-10-05 43 views
-1

我是Python的新手,似乎无法弄清楚为什么.getRow方法不运行。我在dbMain.py中创建了一个DBMain类,我使用pyTest.py来创建DBMain对象来运行getRow。当我在Eclipse中运行调试器时,DBMain的构造函数确实运行,但是当getRow方法调用时没有任何反应。Python方法不能运行在类

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 
+2

cnxn中的getRow不确定。 – Matthias

回答

1
  1. 你这样做从01​​不回报什么。也许你想包括像

    ... 
    return row 
    
  2. getRow()方法不绑定到类。实例方法的签名应该类似于getRow(self) - 第一个参数是显式接收的实例(但在您致电someinstance.method()时隐式传递)。

为了有东西的功能,你或许应该改变你的dbMain到这样的事情:

#@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 

延伸阅读:

+3

他是不是也缺少'getRow(self)'? – Vyktor

+0

我已经返回行的方法和方法仍然没有运行 – Chad

+0

@Chad:你目前的代码是否有'getRow(self)'并且仍然没有运行? –