我正在阅读关于编程集体智慧的搜索引擎的章节,并且遇到了以下代码片段并尝试在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
答案 0 :(得分:0)
您有SQL错误。首先正确格式化SQL。 Here is a quick tutorial。这是我首先要做的语法更改。
fullquery="SELECT %s FROM %s WHERE %s"%(fieldlist,tablelist,clauselist)
换句话说,考虑在SQL关键字上使用大写并重新运行它。