Python 2.7 以下python代码片段中的错误是什么?

Python 2.7 以下python代码片段中的错误是什么?,python-2.7,machine-learning,Python 2.7,Machine Learning,我在阅读《编程集体智能》中有关搜索引擎的章节时,遇到了以下代码片段,并试图在IPython中实现它。但是,我遇到了错误。请帮助 def getmatchrows(self,q): fieldlist='w0.urlid' tablelist='' clauselist='' wordids=[] words=q.split() tablenumber=0 for word in words: wordrow=self.c

我在阅读《编程集体智能》中有关搜索引擎的章节时,遇到了以下代码片段,并试图在IPython中实现它。但是,我遇到了错误。请帮助

def getmatchrows(self,q):
    fieldlist='w0.urlid'
    tablelist=''
    clauselist=''
    wordids=[]

    words=q.split()
    tablenumber=0

    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
    fullquery="select %s from %s where %s"%(fieldlist,tablelist,clauselist)
    cur=self.con.execute(fullquery)
    rows=[row for row in cur]
    return row,wordids
我得到了以下错误:

e.getmatchrows('the')
---------------------------------------------------------------------------
OperationalError                          Traceback (most recent call last)
<ipython-input-4-65d6f760f6c8> in <module>()
----> 1 e.getmatchrows('the')

C:\Users\Blue\Anaconda\searchengine.py in getmatchrows(self, q)
    134                                 tablenumber+=1
    135                 fullquery="select %s from %s where %s"%(fieldlist,tablelist,clauselist)
--> 136                 cur=self.con.execute(fullquery)
    137                 rows=[row for row in cur]
    138                 return row,wordids

OperationalError: near "where": syntax error

您有一个SQL错误。从正确格式化SQL开始。下面是我首先要做的语法更改

fullquery="SELECT %s FROM %s WHERE %s"%(fieldlist,tablelist,clauselist)

换句话说,考虑在你的SQL关键字上使用大写字母并重新运行它。

NOPES,它还没有解决错误。仍然得到同样的错误。它是否显示出相同的错误?你能在编辑器中运行代码吗?'e.getmatchrows'e.getmatchrows中的操作错误'C:\Users\Blue\Anaconda\searchengine.pyc in getmatchrowsself,q 134 tablenumber+=1 135 fullquery=从%s中选择%s,其中%s%fieldlist,tablelist,clauselist 136 cur=self.con.executelquery 137行=[cur中行对行]138返回行,wordid OperationalError:near-where:syntax error'