简体   繁体   English

使用python和sqlite进行关键字搜索

[英]Keyword search using python and sqlite

I have a plugin to a game server that writes down changes made to a map.The database contains entries formatted like this - id INTEGER PRIMARY KEY,matbefore INTEGER, matafter INTEGER, name VARCHAR(50), date DATE. 我有一个游戏服务器的插件,可以记录对地图所做的更改。数据库包含以下格式的条目-id INTEGER PRIMARY KEY,在INTEGER之前,matafter INTEGER,名称VARCHAR(50),日期DATE。 I am trying to create a function that, when given a column name, an integer, string, or tuple of an integer or string, and a keyword, will find the selected entries. 我正在尝试创建一个函数,当给定一个列名,一个整数,字符串或一个整数或字符串的元组以及一个关键字时,该函数将找到所选的条目。 so far, this is the code that I have come to - 到目前为止,这是我要使用的代码-

def readdb(self,keyword,column,returncolumn = "*"):
    self.memwrite
    if isinstance(keyword, int) or isinstance(keyword,str):
        entry = [keyword]
    qmarks = ("? OR " * len(entry))[:-4]
    statement = 'SELECT all {0} FROM main WHERE {1} is {2}'.format(returncolumn,column,qmarks)
    print(qmarks)
    self.memcursor.execute(statement, entry)
    return(self.memcursor.fetchall())

keyword is a keyword to search for, column is teh column to search in, and returncolumn is the column to return So I was wondering why this code always fetches no rows, EG - Returns None, no matter what I put for the function. 关键字是要搜索的关键字,列是要搜索的列,returncolumn是要返回的列,所以我想知道为什么这段代码总是不获取任何行,例如EG-无论我为该函数输入什么都不返回。 It seems to work fine if I do these things in the console, but not if I wrap them in a function 如果我在控制台中执行这些操作,似乎工作正常,但如果将它们包装在函数中,则无法正常工作

If entry is a list (like in yesterday's question) , it's not going to work. 如果entry是一个列表(例如昨天的问题),它将无法正常工作。

>>> returncolumn = "*"
>>> column = "name"
>>> entry = ["Able", "Baker", "Charlie"]
>>> qmarks = ("? OR " * len(entry))[:-4]
>>> statement = 'SELECT all {0} FROM main WHERE {1} is {2}'.format(returncolumn,
column,qmarks)
>>> print statement
SELECT all * FROM main WHERE name is ? OR ? OR ?

and what SQLite will see is: SQLite将看到的是:

SELECT all * FROM main WHERE name is 'Able' OR 'Baker' OR 'Charlie'

which is not valid syntax because you need = , not is . 这是无效的语法,因为你需要= ,不is

Even if you fix that then (using an integer query for example): 即使您随后进行了修复(例如,使用整数查询):

SELECT all * FROM main WHERE id = 1 or 2 or 3

you will get mysterious results because that means WHERE ((id = 1) or 2) or 3) , not what you think it does ... you need WHERE id = 1 or id = 2 or id = 3 or (reverting to yesterday's question) WHERE id IN (1,2,3) 您将获得神秘的结果,因为这意味着WHERE ((id = 1) or 2) or 3) ,而不是您认为的那样...您需要WHERE id = 1 or id = 2 or id = 3或(返回到昨天的问题)在WHERE id IN (1,2,3)

def readdb(self,keyword,column,returncolumn = "*"):
    self.memwrite                                             # 1. 
    if isinstance(keyword, int) or isinstance(keyword,str):   # 2.
        entry = [keyword]                                     # 3.
    qmarks = ("? OR " * len(entry))[:-4]                      # 4.
    statement = 'SELECT all {0} FROM main WHERE {1} is {2}'.format(returncolumn,column,qmarks)
    print(qmarks)                                             # 5.
    self.memcursor.execute(statement, entry)
    return(self.memcursor.fetchall())
  1. Not sure what this is supposed to do. 不知道这应该做什么。 Did you mean self.memwrite()? 您是说self.memwrite()吗?
  2. Can be changed to if isinstance(keyword, (int,str)) 可以更改为if isinstance(keyword, (int,str))

    Or better yet, don't test the type. 或者更好的是,不要测试类型。 As you've written it, keyword can not be a unicode string. 如您所写,关键字不能是unicode字符串。 Why restrict like this? 为什么要这样限制? In this case I think it would be better to use a try...except... block to catch the subsequent error than to restrict type. 在这种情况下,我认为使用try...except...块来捕获后续错误比限制类型更好。

  3. So at best len(entry) = 1 . 所以最多len(entry) = 1 Notice also, its possible to never reach this line if keyword is not of type int or str. 还要注意,如果关键字不是int或str类型,则可能永远不会到达此行。 In that case, you would get an error on line (4) since entry would not be defined.. 在这种情况下,由于不会定义条目,因此在第(4)行上会出现错误。
  4. This could also be written as 这也可以写成

     qmarks = ' OR '.join(['?']*len(entry)) 

    It avoids the need for the somewhat magic number 4 in ("? OR " *1)[:-4] . 它避免了在("? OR " *1)[:-4]使用有些魔术的数字4。

  5. What are you seeing here? 您在这里看到什么? If it was empty, that should have been a clue. 如果它是空的,那应该是一个线索。 Also it would be worth running print(statement) to get a full picture of what is being sent to .execute() . 另外,有必要运行print(statement)以获得发送给.execute()

  6. Perhaps try 也许尝试

     statement = 'SELECT {0} FROM main WHERE {1} = ?'.format( returncolumn,column) 

    In particular, the is should be changed to = . 特别是,应将is更改为=

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM