Python SQLite3:连续两次执行查询时出现奇怪的NoneType问题

时间:2018-05-13 06:30:22

标签: python sqlite flask

所以前言:目标是通过代码将sqlite数据库拆分为三个分片。我有大部分存储和访问数据库的背景,但似乎遇到了一个问题,试图查询一行中的多个分片以找到特定的行。代码可能不言而喻,所以请在下面查看。

def get_db():
"""Opens a new database connection if there is none yet for the
current application context.
"""
    top = _app_ctx_stack.top
    if not hasattr(top, 'sqlite_db'):
        top.sqlite_db = [ sqlite3.connect(DATABASE_1, detect_types=sqlite3.PARSE_DECLTYPES), sqlite3.connect(DATABASE_2, detect_types=sqlite3.PARSE_DECLTYPES), sqlite3.connect(DATABASE_3, detect_types=sqlite3.PARSE_DECLTYPES) ]
        top.sqlite_db[0].row_factory = sqlite3.Row
        top.sqlite_db[1].row_factory = sqlite3.Row
        top.sqlite_db[2].row_factory = sqlite3.Row
    return top.sqlite_db


def query_db(query, shard, args=(), one=False):
"""Queries the database and returns a list of dictionaries."""
    db = get_db()
    cur = db[shard].execute(query, args)
    rv = cur.fetchall()
    return (rv[0] if rv else None) if one else rv


def get_user_id(username):
"""Convenience method to look up the id for a username."""
    rb = query_db('select user_id from user where username = ?', 1, [username], one=True)
    rv = query_db('select user_id from user where username = ?', 2, [username], one=True)
    return rv[0] if rv else None


@app.teardown_appcontext
def close_database(exception):
"""Closes the database again at the end of the request."""
    top = _app_ctx_stack.top
    if hasattr(top, 'sqlite_db'):
        top.sqlite_db[0].close()
        top.sqlite_db[1].close()
        top.sqlite_db[2].close()

我试图包含可能需要的所有内容。 get_user_id是我遇到问题的地方;似乎在箭头中调用query_db两次导致下面的问题(似乎它们被分配给它们并不重要,因为它遇到了同样的问题)。

TypeError: 'NoneType' object has no attribute '__getitem__'

如果我删除其中任何一行(无论是否查询正确的分片)都可以。数据库中是否存在具有该用户名的实际行并不重要,因为当我删除get_user_id中的行时,确认返回None。这个问题似乎是连续两次调用query_db引起的。这进一步让我相信调用数据库连接存在问题。再一次,我试图包含我认为相关的所有内容,但如果这还不够,我可以添加任何其他内容。

编辑1:

按要求添加完整回溯。

Traceback (most recent call last):
  File "/home/me/.local/bin/flask", line 9, in <module>
    load_entry_point('Flask', 'console_scripts', 'flask')()
  File "/home/me/Desktop/project/flask/flask/cli.py", line 881, in main
    cli.main(args=args, prog_name=name)
  File "/home/me/Desktop/project/flask/flask/cli.py", line 557, in main
    return super(FlaskGroup, self).main(*args, **kwargs)
  File "/home/me/.local/lib/python2.7/site-packages/click/core.py", line 697, in main
    rv = self.invoke(ctx)
  File "/home/me/.local/lib/python2.7/site-packages/click/core.py", line 1066, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/me/.local/lib/python2.7/site-packages/click/core.py", line 895, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/me/.local/lib/python2.7/site-packages/click/core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "/home/me/.local/lib/python2.7/site-packages/click/decorators.py", line 17, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/home/me/Desktop/project/flask/flask/cli.py", line 412, in decorator
    return __ctx.invoke(f, *args, **kwargs)
  File "/home/me/.local/lib/python2.7/site-packages/click/core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "/home/me/Desktop/project/flask/examples/minitwit/minitwit/mt_api.py", line 149, in dummy_command
    dummy_db()
  File "/home/me/Desktop/project/flask/examples/minitwit/minitwit/mt_api.py", line 143, in dummy_db
    rv = get_user_id('dbrewer5')
  File "/home/me/Desktop/project/flask/examples/minitwit/minitwit/mt_api.py", line 86, in get_user_id
    rv = query_db('select user_id from user where username = ?', 2, [username], one=True)
  File "/home/me/Desktop/project/flask/examples/minitwit/minitwit/mt_api.py", line 75, in query_db
    cur = db[shard].execute(query, args)
TypeError: 'NoneType' object has no attribute '__getitem__'

0 个答案:

没有答案