sqlalchemy ======》 row to dict

recipients_query = db.session.query(BIUser).filter(BIUser.user_id.in_(user_ids)).all()
                
data = [{'user_id': row.user_id, 'username': row.username, 'country': row.reg_country, 'email': row.email} for row in recipients_query]

使用orm

def row_to_dict(row):
    result = {}
    for column in row.__table__.columns:
        result[column.name] = str(getattr(row, column.name))
    return result


query_result = db.session.query(BIUserStatistic_Model_Class).all() 


[  row_to_dict(row) for row   in query_result ]


```

##使用原生sql

```
query_result = db.engine.execute(text("""
                                       SELECT new_reg
                                       FROM   bi_statistic
                                       WHERE  on_day = :end_time
                                              AND game = :game
                                       """), start_time=start_time, end_time=end_time, game='All Game').fetchall()


 [dict(row.items()) for row in result]

```

你可能感兴趣的:(sqlalchemy ======》 row to dict)