使用Web.py搜索字符串

时间:2015-11-22 21:28:50

标签: python sql web.py

我尝试使用web.py和SQLite构建一个python函数,允许用户在描述字段中搜索给定的字符串,并返回所有匹配的结果。

现在我已经使用了以下功能,只有在输入完全匹配时才有效。

def getItem(params, max_display):
    query_string = 'SELECT * FROM items WHERE 1=1'
    description = params['description']

    if params['description']:
        query_string = query_string + ' AND description LIKE $description'
result = query(query_string, {
    'description': params['description']

我已尝试使用LIKE "%$description%"'实现此功能,但我不断收到以下web.py错误。

Traceback (most recent call last):
  File "lib/web/wsgiserver/__init__.py", line 1245, in communicate
    req.respond()
  File "lib/web/wsgiserver/__init__.py", line 775, in respond
    self.server.gateway(self).respond()
  File "lib/web/wsgiserver/__init__.py", line 2018, in respond
    response = self.req.server.wsgi_app(self.env, self.start_response)
  File "lib/web/httpserver.py", line 306, in __call__
    return self.app(environ, xstart_response)
  File "lib/web/httpserver.py", line 274, in __call__
    return self.app(environ, start_response)
  File "lib/web/application.py", line 279, in wsgi
    result = self.handle_with_processors()
  File "lib/web/application.py", line 249, in handle_with_processors
    return process(self.processors)
  File "lib/web/application.py", line 246, in process
    raise self.internalerror()
  File "lib/web/application.py", line 478, in internalerror
    return debugerror.debugerror()
  File "lib/web/debugerror.py", line 305, in debugerror
    return web._InternalError(djangoerror())
  File "lib/web/debugerror.py", line 290, in djangoerror
    djangoerror_r = Template(djangoerror_t, filename=__file__, filter=websafe)
  File "lib/web/template.py", line 846, in __init__
    code = self.compile_template(text, filename)
  File "lib/web/template.py", line 926, in compile_template
    ast = compiler.parse(code)
  File "/Users/sokeefe/homebrew/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/compiler/transformer.py", line 51, in parse
    return Transformer().parsesuite(buf)
  File "/Users/sokeefe/homebrew/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/compiler/transformer.py", line 128, in parsesuite
    return self.transform(parser.suite(text))
AttributeError: 'module' object has no attribute 'suite'

有关此功能可能出现的问题的任何想法?

提前致谢!

1 个答案:

答案 0 :(得分:1)

  

您认为parser.py会怎么样?

以下是错误消息的相关部分:

  

文件   “/Users/sokeefe/homebrew/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/compiler/transformer.py”   第128行,在parsesuite中       return self.transform( parser.suite (text))AttributeError:'module'对象没有属性'suite'

因此,某处有一个名为parser.py的文件,它定义了一个名为suite()的函数,该函数由程序执行时执行的某些库代码使用。但是因为你命名了一个文件parser.py,当库代码执行时,python搜索一个名为parser.py的文件,而python首先找到你的文件,并且你的文件中没有名为suite()的函数。