我有很多數據庫查詢,我想用一些方法來不重複我的代碼。我想打電話給其他定義的方法方法,但它不工作在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
這裏是您能夠使用自己的類? – Netwave
類缺少自我。 – Riyaz
班級在哪裏? – tglaria