sqlite3 dict_factory和PyCharm的问题

时间:2015-12-07 20:57:30

标签: python python-3.x sqlite pycharm

我试图在这里遵循正确的API文档:https://docs.python.org/2/library/sqlite3.html让我的fetchall()通过使用dict_factory来返回字典列表:

def dict_factory(cursor, row):
    d = {}
    for idx, col in enumerate(cursor.description):
        d[col[0]] = row[idx]
    return d

稍后在我的代码中引用:

class Competitor:

    def __init__(self, db_file="BusinessDB"):
        self.db_exists = os.path.exists(db_file)
        self.db = sqlite3.connect(db_file)
        self.db.row_factory = dict_factory

        self.cursor = self.db.cursor()
        self.cursor.execute("SELECT * FROM competitors")

        self.competitors = self.cursor.fetchall()

        self.cursor.close()

        self.current_key = self.competitors[0]['CompetitorID']

        ....

然而,PyCharm不断给予"预期类型'积分'得到' str'代替"对于我的竞争对手'参考。我怀疑PyCharm并不认识自己的竞争对手是一个字典列表。如何重构此代码在技术上是正确的?

由于

0 个答案:

没有答案