当前位置:   article > 正文

mysql数据表自动导为python sqlalchemy可操作对象_mysql表转成python类

mysql表转成python类

1、pip install sqlacodegen

      pip install pymysql

     在/usr/lib/python/site-packages/sqlacodegen/main.py中添加:

          import pymysql

         pymysql.install_as_MySQLdb()


2、创建review_models.sh文件,在文件中添加:

      #!/usr/bin/env bash
      sqlacodegen --noviews --noconstraints --outfile=models.py mysql://iips:iips@192.168.1.200:3306/iips


3、执行上面shell文件,将在当前目录下输出models.py,数据库iips中的表结构将转换成为sqlalchemy可操作的类对象,如下:

# coding: utf-8
from sqlalchemy import Column, DateTime, Integer, String
from sqlalchemy.ext.declarative import declarative_base


Base = declarative_base()
metadata = Base.metadata


class TBuilding(Base):
    __tablename__ = 't_building'

    id = Column(Integer, primary_key=True)
    building_code = Column(String(20), nullable=False, unique=True)
    land_code = Column(String(20), nullable=False, index=True)
    building_name = Column(String(40))
    building_area = Column(Integer)
    rent_area = Column(Integer)
    one_floor_area = Column(Integer, nullable=False)
    total_floors = Column(Integer, nullable=False)
    floor_hight = Column(Integer, nullable=False)
    loadbearing = Column(Integer, nullable=False)
    status = Column(Integer, nullable=False)
    structure = Column(String(40))

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/613292
推荐阅读
相关标签
  

闽ICP备14008679号