美文网首页
sqlalchemy ======》 row to dict

sqlalchemy ======》 row to dict

作者: 樊海鹏 | 来源:发表于2017-07-04 11:30 被阅读0次
    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

          本文链接:https://www.haomeiwen.com/subject/kjwthxtx.html