我的代码使用func“ vk.profile_stats”给了我这个错误:

(pymysql.err.OperationalError) (2006, "MySQL server has gone away (BrokenPipeError(32, 'Broken pipe'))")
[SQL: SELECT vk_profile.id AS vk_profile_id, vk_profile.friends AS vk_profile_friends, vk_profile.followers AS vk_profile_followers, vk_profile.audios AS vk_profile_audios, vk_profile.gifts AS vk_profile_gifts, vk_profile.date AS vk_profile_date
FROM vk_profile
WHERE vk_profile.date = %(date_1)s
 LIMIT %(param_1)s]
[parameters: {'date_1': datetime.date(2019, 11, 27), 'param_1': 1}]
(Background on this error at: http://sqlalche.me/e/e3q8)


我确定该函数的输入是正确的。该功能:

def profile_stats():
    profile = vk.users.get(fields='counters')[0]['counters']
    stats.vk.write_profile(profile['friends'], profile['followers'], profile['audios'], profile['gifts'])


功能“ stats.vk.write_profile”:

def write_profile(friends, followers, audios, gifts):
    session = Session()
    profile = session.query(Vk_profile).filter(Vk_profile.date == date.today()).first()
    if profile:
        profile.friends = friends
        profile.followers = followers
        profile.audios = audios
        profile.gifts = gifts
    else:
        profile = Vk_profile(friends, followers, audios, gifts)
        session.add(profile)
    session.commit()
    session.close()


该模型:

class Vk_profile(Base):
    __tablename__ = 'vk_profile'
    id = db.Column(db.Integer, primary_key=True)
    friends = db.Column(db.Integer)
    followers = db.Column(db.Integer)
    audios = db.Column(db.Integer)
    gifts = db.Column(db.Integer)
    date = db.Column(db.Date)

    def __init__(self, friends, followers, audios, gifts):
        self.friends = friends
        self.followers = followers
        self.audios = audios
        self.gifts = gifts
        self.date = date.today()

最佳答案

您创建数据库了吗? db.create_all()

10-08 09:05