2015-12-24 146 views
-2

我有很多數據庫查詢,我想用一些方法來不重複我的代碼。我想打電話給其他定義的方法方法,但它不工作在Python中調用另一種方法

我得到這樣的錯誤:

class Main: 
    File "d.py", line 20, in Main 
    for word in getUserWords("SELECT users.mail, field_data_field_what_word_are_you_looking_.field_what_word_are_you_looking__value, users.uid FROM users INNER JOIN field_data_field_what_word_are_you_looking_ ON users.uid = field_data_field_what_word_are_you_looking_.entity_id"): 
TypeError: getUserWords() takes exactly 2 arguments (1 given) 

我的代碼

import MySQLdb as mdb 
Class Main: 


    def connect(self): 
     con = mdb.connect('***', '*****', '****', '***', charset="utf8", use_unicode=True) 
     return con 

    def cursor(self): 
     cursor = self.connect.cursor() 
     return cursor() 

    def getUserWords(self, sql): 
     self.sql = sql 
     self.cursor.execute(self.sql) 
     data = self.cursor.fetchall() 
     self.connect.commit() 
     self.connect.close() 
     return data 

    for word in getUserWords("SELECT users.mail, field_data_field_what_word_are_you_looking_.field_what_word_are_you_looking__value, users.uid FROM users INNER JOIN field_data_field_what_word_are_you_looking_ ON users.uid = field_data_field_what_word_are_you_looking_.entity_id"): 
     print word 
+2

這裏是您能夠使用自己的類? – Netwave

+2

類缺少自我。 – Riyaz

+0

班級在哪裏? – tglaria

回答

2

簡單的例子:

class Foo(object): 
    def __init__(self): 
     self.foo = "bar" 
    def function1(self,x): 
     self.function2(x) 
    def function2(self,y): 
     print y 

bar = Foo() 
bar.function1(3) # calls function1 which in turn calls function2 which prints out 3 
bar.function2(4) # calls function 2 directly. 

回答你的問題的主要功能:

如果您有一個類函數,它具有第一個參數,它是通過慣例自我。如果你在一個實例上調用這個類函數(如bar.function2),那麼這個self就是隱含的。如果從類內部調用該類函數(就像函數1調用函數2時那樣),則需要執行self.functionname,該函數再次隱式傳遞self參數。

0

第一點:實例化類,並在你的實例調用getUserWords()

import MySQLdb as mdb 
class Main: 
    # snip 


m = Main() 
sql = your_sql_here 
for word in m.getUserWords(sql): 
    print word 

二點:你的Main實現是有缺陷的。

Class Main: 
    def connect(self): 
     # this will open a new connection on each and every call 

     con = mdb.connect('***', '*****', '****', '***', charset="utf8", use_unicode=True) 
     return con 

    def cursor(self): 
     # this will 
     # 1. create a new connection on every call - which will 
     # never be closed since you don't keep a reference 
     # on it so you can close it 
     # 2. create a new cursor on every call 
     cursor = self.connect.cursor() 

     # and this one will raise a TypeError 
     # => "'Cursor' object is not callable" 
     return cursor() 
     # so I assume your real code is : 
     return cursor 

    def getUserWords(self, sql): 
     # assigning sql to self is totally useless here 
     self.sql = sql 

     # so (assuming self.cursor returns the cursor and not 
     # the call to the cursor), this will: 
     # - open a new connection 
     # - create a new cursor 
     # - execute the sql 
     # - and discards eveything (cursor and connection) 
     # without closing them 
     self.cursor.execute(self.sql) 

     # now we 
     # - open a second connection (without closing the first) 
     # - create a second cursor 
     # - call .fetchall() on it, which will raise a 
     # _mysql_exceptions.ProgrammingError 
     data = self.cursor.fetchall() 

     # we're not making it until this part because of 
     # the above error, but if we did, this would: 
     # - create yet a third connection and call .commit() 
     # on it - which in this case would mainly be a no-op 
     # since we have nothing to commit 
     self.connect.commit() 

     # and finally create a fourth connection and close it 
     # immediatly - note that this will be the only one that 
     # gets closed <g> 
     self.connect.close() 
     return data 

你的代碼的固定版本可以是這個樣子:

import MySQLdb as mdb 

class Main(object): 

    def __init__(self, connection_data): 
     self._connection_data = connection_data.copy() 
     self._connection_data.update(charset="utf8", use_unicode=True) 
     self._db = None 

    @property 
    def db(self): 
     if self._db is None: 
      self._db = mdb.connect(**self._connection_data) 
     return self._db 

    def cursor(self): 
     return self.db.cursor() 

    def execute(self, sql): 
     cursor = self.cursor() 
     cursor.execute(self.sql) 
     for row in cursor: 
      yield row 
     self.db.commit() 
     cursor.close() 


    def __del__(self): 
     try: 
      self._db.close() 
     except: 
      # either it's not set or it's already closed 
      pass 


m = Main(db="***", user="***", passwd="***") 
for w in m.getUserWords(your_sql_here): 
    print w 
相關問題