2015-12-24 31 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