NameError: name 'of class' is not defined
NameError: name 'of class' is not defined
我写了一个 class(搜索器)用于在数据库中搜索一串单词但是当我想执行我的脚本时我得到这个错误:
NameError: name 'searcher' is not defined
我的代码:
class searcher:
def __init__(self, dbname):
self.con = sqlite3.connect(dbname)
def __del__(self):
self.con.close()
def getmatchrows(self,q):
fieldlist = 'w0.urlid'
tablelist = ''
clauselist = ''
words = q.split(' ')
tablenumber = 0
wordids = []
for word in words:
wordrow = self.con.execute("select rowid from wordlist where word = '%s'" %word).fetchone()
if wordrow !=None:
wordid = wordrow[0]
wordids.append(wordid)
if tablenumber > 0:
tablelist+=','
clauselist+=' and '
clauselist+='w%d.urlid=w%d.urlid and ' % (tablenumber -1, tablenumber)
fieldlist+=',w%d.location'% tablenumber
tablelist+='wordlocation w%d'% tablenumber
clauselist+='w%d.wordid = %d' % (tablenumber,wordid)
tablenumber+=1
query = 'select %s from %s where %s' % (fieldlist,tablelist,clauselist)
cur = self.con.execute(query)
rows = [row for row in cur]
return rows,wordids
wordsearch = searcher('searchindex.db')
print wordsearch.getmatchrows('indie music')
我哪里做错了?!!
代码的最后两行是缩进的,因此它们属于 class searcher:
块。但是,searcher
class 直到此块结束后才存在,因此尝试在 wordsearch = searcher('searchindex.db')
中引用 searcher
失败。
取消最后两行的缩进。
我写了一个 class(搜索器)用于在数据库中搜索一串单词但是当我想执行我的脚本时我得到这个错误:
NameError: name 'searcher' is not defined
我的代码:
class searcher:
def __init__(self, dbname):
self.con = sqlite3.connect(dbname)
def __del__(self):
self.con.close()
def getmatchrows(self,q):
fieldlist = 'w0.urlid'
tablelist = ''
clauselist = ''
words = q.split(' ')
tablenumber = 0
wordids = []
for word in words:
wordrow = self.con.execute("select rowid from wordlist where word = '%s'" %word).fetchone()
if wordrow !=None:
wordid = wordrow[0]
wordids.append(wordid)
if tablenumber > 0:
tablelist+=','
clauselist+=' and '
clauselist+='w%d.urlid=w%d.urlid and ' % (tablenumber -1, tablenumber)
fieldlist+=',w%d.location'% tablenumber
tablelist+='wordlocation w%d'% tablenumber
clauselist+='w%d.wordid = %d' % (tablenumber,wordid)
tablenumber+=1
query = 'select %s from %s where %s' % (fieldlist,tablelist,clauselist)
cur = self.con.execute(query)
rows = [row for row in cur]
return rows,wordids
wordsearch = searcher('searchindex.db')
print wordsearch.getmatchrows('indie music')
我哪里做错了?!!
代码的最后两行是缩进的,因此它们属于 class searcher:
块。但是,searcher
class 直到此块结束后才存在,因此尝试在 wordsearch = searcher('searchindex.db')
中引用 searcher
失败。
取消最后两行的缩进。