将“计数”列表从第一个函数拉到第二个函数中以更新表

时间:2015-11-01 00:28:48

标签: python sqlalchemy

尝试将第一个函数中的“计数”列表拉到第二个函数中以更新表。

我正在使用Python,SQLAlchemy,sqlite。我觉得问题出在我的python中。它是列表,元组?我该怎么做?

def countCurrentOccupancy():
    p = session.query(Puppy.shelter_id, func.count(Puppy.shelter_id))
    grouped_count = p.group_by (Puppy.shelter_id).all()
    return grouped_count


def updateCurrentOccupancy():
    l = countCurrentOccupancy()
    list_c = list(l)
    s = session.query(Shelter.shelter_id, Shelter.current_occupancy)
    print 'updateCurrentOccupancy:'
    print '----------------------'
    for row_s in s:
        row_s = list_c #Trying to pull matching data from countCurrentOccupancy(), will update to an integer that I choose but that is not helpful.
        print row_s
    session.commit(row_s)
    showShelter()

结果:

TypeError: commit() takes exactly 1 argument (2 given)

1 个答案:

答案 0 :(得分:0)

您可以将列表转换为dict {shelter_id:count}并通过count获取shelter_id

def updateCurrentOccupancy():
    counts_list = countCurrentOccupancy()
    counts_dict = dict(counts_list)

    shelters = session.query(Shelter)
    for shelter in shelters:
        shelter.current_occupancy = counts_dict.get(shelter.id)
        session.add(shelter)

    session.commit()