mysql数据表自动导为python sqlalchemy可操作对象

python 从数据库表生成model

找了很久才找到这个,我是新手...

现在已有建好的数据库,需要基于原有数据做数据分析的web应用,我选择python+Tornado ,由于不想写SQL语句,就想偷个懒

1、安装工具

1 [email protected]:~/code/py/django/logcloud$ sudo pip install sqlacodegen
2 Downloading/unpacking sqlacodegen
3   Downloading sqlacodegen-1.1.6-py2.py3-none-any.whl
4 Downloading/unpacking inflect>=0.2.0 (from sqlacodegen)
5   Downloading inflect-0.2.5-py2.py3-none-any.whl (58kB): 58kB downloaded
6 Requirement already satisfied (use --upgrade to upgrade): SQLAlchemy>=0.6.0 in /usr/local/lib/python2.7/dist-packages (from sqlacodegen)
7 Installing collected packages: sqlacodegen, inflect
8 Successfully installed sqlacodegen inflect
9 Cleaning up...

2、转换

[email protected]:~/code/py/django/logcloud$ sqlacodegen mssql+pymssql://name:[email protected]/LogColudDB --outfile logcloude_model.py
[email protected]:~/code/py/django/logcloud$

3、查看,哈哈

[email protected]:~/code/py/django/logcloud$ cat logcloude_model.py
# coding: utf-8
from sqlalchemy import BigInteger, Column, DateTime, Float, ForeignKey, Integer, LargeBinary, T                   able, Unicode, text
from sqlalchemy.dialects.mssql.base import BIT
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()
metadata = Base.metadata

class AuthorityInfo(Base):
    __tablename__ = ‘AuthorityInfo‘

    ID = Column(BigInteger, primary_key=True)
    Description = Column(Unicode(‘max‘), nullable=False)
    IsDelete = Column(BIT, nullable=False)
    AuthorityIndex = Column(Integer, nullable=False)
    AuthorityName = Column(Unicode(‘max‘), nullable=False)

class CPKInfoHistory(Base):
    __tablename__ = ‘CPKInfoHistory‘

    PO = Column(Unicode(10), primary_key=True)
    ProcessName = Column(Unicode(50), nullable=False)
    Result = Column(Unicode(‘max‘), nullable=False)
    LastLogID = Column(BigInteger, nullable=False)

class ComputerState(Base):
    __tablename__ = ‘ComputerState‘

    Name = Column(Unicode(50), primary_key=True)
    Ip = Column(Unicode(50), nullable=False)
    IsDelete = Column(BIT, nullable=False)
    LastDate = Column(DateTime, nullable=False)
    IsProceted = Column(BIT, nullable=False)

class DeviceInfo(Base):
    __tablename__ = ‘DeviceInfo‘

    ID = Column(BigInteger, primary_key=True)
    ClientName = Column(Unicode(50), nullable=False)
    Description = Column(Unicode(1024))
    IsDelete = Column(BIT, nullable=False)
    LinesInfo_ID = Column(ForeignKey(u‘LinesInfo.ID‘), nullable=False, index=True)

    LinesInfo = relationship(u‘LinesInfo‘)

class DisposeErrorCodeInfo(Base):
    __tablename__ = ‘DisposeErrorCodeInfo‘

    ID = Column(BigInteger, primary_key=True)
    NewOperation = Column(Unicode(‘max‘), nullable=False)
    Status = Column(Unicode(‘max‘), nullable=False)
    DisposeInfo_ID = Column(ForeignKey(u‘DisposeInfo.ID‘), nullable=False, index=True)
    ErrorCode_Info_ID = Column(ForeignKey(u‘ErrorCode_Info.ID‘), nullable=False, index=True)
    ErrorCode_OperationID = Column(BigInteger, nullable=False)
    WeightValue = Column(Integer, nullable=False)

    DisposeInfo = relationship(u‘DisposeInfo‘)
    ErrorCode_Info = relationship(u‘ErrorCodeInfo‘)

class DisposeErrorCodeInfoHistory(Base):
    __tablename__ = ‘DisposeErrorCodeInfoHistory‘

    ID = Column(BigInteger, primary_key=True)
    OperateTime = Column(DateTime, nullable=False)
    OldStatus = Column(Unicode(‘max‘), nullable=False)
    NewStatus = Column(Unicode(‘max‘), nullable=False)
    IsDelete = Column(BIT, nullable=False)
    WeightValue = Column(Integer, nullable=False)
    UserInfo_ID = Column(ForeignKey(u‘UserInfo.ID‘), nullable=False, index=True)
    DisposeErrorCodeInfo_ID = Column(ForeignKey(u‘DisposeErrorCodeInfo.ID‘), nullable=False, in                   dex=True)

    DisposeErrorCodeInfo = relationship(u‘DisposeErrorCodeInfo‘)
    UserInfo = relationship(u‘UserInfo‘)

class DisposeInfo(Base):
    __tablename__ = ‘DisposeInfo‘

    ID = Column(BigInteger, primary_key=True)
    Operation = Column(Unicode(‘max‘), nullable=False)
    IsOK = Column(BIT, nullable=False)
    Description = Column(Unicode(1024))
    TouchTime = Column(DateTime, nullable=False)
    NoticeInfo_ID = Column(ForeignKey(u‘NoticeInfo.ID‘), nullable=False, index=True)
    UserTask_ID = Column(ForeignKey(u‘UserTask.ID‘), nullable=False, index=True)

    NoticeInfo = relationship(u‘NoticeInfo‘)
    UserTask = relationship(u‘UserTask‘)

class ErrorCodeInfo(Base):
    __tablename__ = ‘ErrorCode_Info‘

    ID = Column(BigInteger, primary_key=True)
    ErrorCode = Column(Unicode(10), nullable=False)
    Description = Column(Unicode(1024))
    IsDelete = Column(BIT, nullable=False)

class ErrorCodeOperation(Base):
    __tablename__ = ‘ErrorCode_Operation‘

    ID = Column(BigInteger, primary_key=True)
    Operation = Column(Unicode(‘max‘), nullable=False)
    WeightValue = Column(Integer, nullable=False)
    IsEnable = Column(BIT, nullable=False)
    ErrorCode_Info_ID = Column(ForeignKey(u‘ErrorCode_Info.ID‘), nullable=False, index=True)

    ErrorCode_Info = relationship(u‘ErrorCodeInfo‘)

class FilesManage(Base):
    __tablename__ = ‘FilesManage‘

    ID = Column(BigInteger, primary_key=True)
    ClassName = Column(Unicode(50), nullable=False, index=True)
    Md5 = Column(Unicode(32), nullable=False)
    Data = Column(LargeBinary, nullable=False)
    Ver = Column(Integer, nullable=False)
    DateCreated = Column(Unicode(50), nullable=False)
    UpLoadUserName = Column(Unicode(50), nullable=False)
    Remarks = Column(Unicode(‘max‘))
    Catagory = Column(Unicode(50), nullable=False)
    LocalFileName = Column(Unicode(50))

class LOGInfo(Base):
    __tablename__ = ‘LOG_Info‘

    ID = Column(BigInteger, primary_key=True)
    Po = Column(Unicode(10), nullable=False, index=True)
    ProcessName = Column(Unicode(10), nullable=False, index=True)
    User = Column(Unicode(10), nullable=False)
    ErrorCode = Column(Unicode(10))
    Log = Column(Unicode(‘max‘), nullable=False)
    Barcode = Column(Unicode(50))
    Isn = Column(Unicode(50))
    Shift = Column(Unicode(10), nullable=False)
    TestResult = Column(Unicode(10), nullable=False, index=True)
    LastDate = Column(DateTime, nullable=False)
    ClientName = Column(Unicode(50), nullable=False, index=True)

class LinesInfo(Base):
    __tablename__ = ‘LinesInfo‘

    ID = Column(BigInteger, primary_key=True)
    Name = Column(Unicode(50), nullable=False)
    Description = Column(Unicode(1024))
    IsDelete = Column(BIT, nullable=False)

class NoticeInfo(Base):
    __tablename__ = ‘NoticeInfo‘

    ID = Column(BigInteger, primary_key=True)
    Top1_ErrorCodeID = Column(BigInteger, nullable=False)
    Top2_ErrorCodeID = Column(BigInteger)
    Top3_ErrorCodeID = Column(BigInteger)
    TouchTime = Column(DateTime, nullable=False)
    IsDispose = Column(BIT, nullable=False)
    TaskLavel = Column(Integer, nullable=False)
    Responsibility_ID = Column(ForeignKey(u‘Responsibility.ID‘), nullable=False, index=True)
    UserTask_ID = Column(ForeignKey(u‘UserTask.ID‘), nullable=False, index=True)
    DeviceInfo_ID = Column(ForeignKey(u‘DeviceInfo.ID‘), nullable=False, index=True)
    StatisticsInfo_ID = Column(BigInteger, nullable=False)

    DeviceInfo = relationship(u‘DeviceInfo‘)
    Responsibility = relationship(u‘Responsibility‘)
    UserTask = relationship(u‘UserTask‘)

class POInfo(Base):
    __tablename__ = ‘POInfo‘

    ID = Column(BigInteger, primary_key=True)
    Po = Column(Unicode(10), nullable=False)
    Plm = Column(Unicode(20))
    ProductName = Column(Unicode(50))
    Description = Column(Unicode(1024))
    IsDelete = Column(BIT, nullable=False)
    Customer = Column(Unicode(50))

class Responsibility(Base):
    __tablename__ = ‘Responsibility‘

    ID = Column(BigInteger, primary_key=True)
    ProcessName = Column(Unicode(10), nullable=False)
    BaseNumber = Column(Integer, nullable=False)
    ErrorRate = Column(Float(53), nullable=False)
    Description = Column(Unicode(1024))
    OverTime = Column(Integer, nullable=False)
    POInfo_ID = Column(ForeignKey(u‘POInfo.ID‘), nullable=False, index=True)
    UserInfo_ID = Column(ForeignKey(u‘UserInfo.ID‘), nullable=False, index=True)

    POInfo = relationship(u‘POInfo‘)
    UserInfo = relationship(u‘UserInfo‘)

class RoleAuthority(Base):
    __tablename__ = ‘RoleAuthority‘

    ID = Column(BigInteger, primary_key=True)
    IsDelete = Column(BIT, nullable=False)
    RoleInfo_ID = Column(ForeignKey(u‘RoleInfo.ID‘), nullable=False, index=True)
    AuthorityInfo_ID = Column(ForeignKey(u‘AuthorityInfo.ID‘), nullable=False, index=True)

    AuthorityInfo = relationship(u‘AuthorityInfo‘)
    RoleInfo = relationship(u‘RoleInfo‘)

class RoleInfo(Base):
    __tablename__ = ‘RoleInfo‘

    ID = Column(BigInteger, primary_key=True)
    RoleName = Column(Unicode(256), nullable=False)
    Description = Column(Unicode(1024))
    IsDelete = Column(BIT, nullable=False)
    RoleLevel = Column(Integer, nullable=False)

class SettingInfo(Base):
    __tablename__ = ‘SettingInfo‘

    ID = Column(BigInteger, primary_key=True, nullable=False)
    Key = Column(Unicode(50), primary_key=True, nullable=False)
    Value = Column(Unicode(‘max‘), nullable=False)
    Description = Column(Unicode(1024))

class StatisticsInfo(Base):
    __tablename__ = ‘StatisticsInfo‘

    ID = Column(BigInteger, primary_key=True)
    ProcessName = Column(Unicode(10), nullable=False)
    BeginTime = Column(DateTime, nullable=False)
    NowErrorRate = Column(Float(53), nullable=False)
    Times = Column(Integer, nullable=False)
    IsOutmoded = Column(BIT, nullable=False)
    POInfo_ID = Column(ForeignKey(u‘POInfo.ID‘), nullable=False, index=True)
    DeviceInfo_ID = Column(ForeignKey(u‘DeviceInfo.ID‘), nullable=False, index=True)

    DeviceInfo = relationship(u‘DeviceInfo‘)
    POInfo = relationship(u‘POInfo‘)

class UserInfo(Base):
    __tablename__ = ‘UserInfo‘

    ID = Column(BigInteger, primary_key=True)
    Name = Column(Unicode(10), nullable=False)
    JobNumber = Column(Unicode(10), nullable=False)
    Phone = Column(Unicode(20))
    Emil = Column(Unicode(30), nullable=False)
    Department = Column(Unicode(20))
    Duties = Column(Unicode(20))
    Description = Column(Unicode(1024))
    Group = Column(Unicode(20), nullable=False)
    IsDelete = Column(BIT, nullable=False)
    Password = Column(Unicode(32))
    CreateDateTime = Column(DateTime, nullable=False)

class UserProfile(Base):
    __tablename__ = ‘UserProfile‘

    UserId = Column(Integer, primary_key=True)
    UserName = Column(Unicode(56), nullable=False, unique=True)

class UserRoleInfo(Base):
    __tablename__ = ‘UserRoleInfo‘

    ID = Column(BigInteger, primary_key=True)
    IsDelete = Column(BIT, nullable=False)
    UserInfo_ID = Column(ForeignKey(u‘UserInfo.ID‘), nullable=False, index=True)
    RoleInfo_ID = Column(ForeignKey(u‘RoleInfo.ID‘), nullable=False, index=True)

    RoleInfo = relationship(u‘RoleInfo‘)
    UserInfo = relationship(u‘UserInfo‘)

class UserTask(Base):
    __tablename__ = ‘UserTask‘

    ID = Column(BigInteger, primary_key=True)
    TaskLavel = Column(Integer, nullable=False)
    Shift = Column(Unicode(10), nullable=False)
    Description = Column(Unicode(1024))
    IsDelete = Column(BIT, nullable=False)
    UserInfo_ID = Column(ForeignKey(u‘UserInfo.ID‘), nullable=False, index=True)
    LinesInfo_ID = Column(ForeignKey(u‘LinesInfo.ID‘), nullable=False, index=True)

    LinesInfo = relationship(u‘LinesInfo‘)
    UserInfo = relationship(u‘UserInfo‘)

t_view_LinesState = Table(
    ‘view_LinesState‘, metadata,
    Column(‘ID‘, BigInteger, nullable=False),
    Column(‘ClientName‘, Unicode(50), nullable=False),
    Column(‘LinesInfo_ID‘, BigInteger, nullable=False),
    Column(‘count‘, Integer)
)

t_view_LogState = Table(
    ‘view_LogState‘, metadata,
    Column(‘Po‘, Unicode(10), nullable=False),
    Column(‘ProcessName‘, Unicode(10), nullable=False),
    Column(‘ErrorCode‘, Unicode(10)),
    Column(‘ClientName‘, Unicode(50), nullable=False),
    Column(‘ID‘, BigInteger),
    Column(‘count‘, Integer)
)

t_view_NoticeInfo = Table(
    ‘view_NoticeInfo‘, metadata,
    Column(‘ID‘, BigInteger, nullable=False),
    Column(‘TouchTime‘, DateTime, nullable=False),
    Column(‘Top1_ErrorCodeID‘, BigInteger, nullable=False),
    Column(‘Top2_ErrorCodeID‘, BigInteger),
    Column(‘Top3_ErrorCodeID‘, BigInteger),
    Column(‘IsDispose‘, BIT, nullable=False),
    Column(‘TaskLavel‘, Integer, nullable=False),
    Column(‘ClientName‘, Unicode(50)),
    Column(‘ProcessName‘, Unicode(10)),
    Column(‘BeginTime‘, DateTime),
    Column(‘NowErrorRate‘, Float(53)),
    Column(‘Times‘, Integer),
    Column(‘IsOutmoded‘, BIT),
    Column(‘Po‘, Unicode(10))
)

t_view_Top1_error = Table(
    ‘view_Top1_error‘, metadata,
    Column(‘Top1_ErrorCodeID‘, BigInteger, nullable=False),
    Column(‘count‘, Integer),
    Column(‘ID‘, BigInteger, nullable=False),
    Column(‘ErrorCode‘, Unicode(10), nullable=False),
    Column(‘Description‘, Unicode(1024))
)

class WebpagesMembership(Base):
    __tablename__ = ‘webpages_Membership‘

    UserId = Column(Integer, primary_key=True)
    CreateDate = Column(DateTime)
    ConfirmationToken = Column(Unicode(128))
    IsConfirmed = Column(BIT, server_default=text("((0))"))
    LastPasswordFailureDate = Column(DateTime)
    PasswordFailuresSinceLastSuccess = Column(Integer, nullable=False, server_default=text("((0                   ))"))
    Password = Column(Unicode(128), nullable=False)
    PasswordChangedDate = Column(DateTime)
    PasswordSalt = Column(Unicode(128), nullable=False)
    PasswordVerificationToken = Column(Unicode(128))
    PasswordVerificationTokenExpirationDate = Column(DateTime)

class WebpagesOAuthMembership(Base):
    __tablename__ = ‘webpages_OAuthMembership‘

    Provider = Column(Unicode(30), primary_key=True, nullable=False)
    ProviderUserId = Column(Unicode(100), primary_key=True, nullable=False)
    UserId = Column(Integer, nullable=False)

class WebpagesRole(Base):
    __tablename__ = ‘webpages_Roles‘

    RoleId = Column(Integer, primary_key=True)
    RoleName = Column(Unicode(256), nullable=False, unique=True)

    UserProfile = relationship(u‘UserProfile‘, secondary=‘webpages_UsersInRoles‘)

t_webpages_UsersInRoles = Table(
    ‘webpages_UsersInRoles‘, metadata,
    Column(‘UserId‘, ForeignKey(u‘UserProfile.UserId‘), primary_key=True, nullable=False),
    Column(‘RoleId‘, ForeignKey(u‘webpages_Roles.RoleId‘), primary_key=True, nullable=False)
)
[email protected]:~/code/py/django/logcloud$

原文地址:https://www.cnblogs.com/pejsidney/p/9217168.html

时间: 2024-10-11 11:20:30

mysql数据表自动导为python sqlalchemy可操作对象的相关文章

使用命令设置MySQL数据表自增ID起始值

使用命令设置MySQL数据表自增ID起始值技术 maybe yes 发表于2015-01-24 16:14 原文链接 : http://blog.lmlphp.com/archives/68  来自 : LMLPHP后院 有 时候我们清空了 MySQL 数据库中数据表的记录,自动增长的 ID 值变的很大,如何将自动增长的 ID 值设置为1或者修改为其他的值呢?使用一些工具,比如 NaviCat for MySQL 当然非常简单,通过在设计表处修改即可,其他的一些工具也都很简单.下面给出使用 SQ

MyBatis 逆向工程——根据数据表自动生成model、xml映射文件、mapper接口

MyBatis Generator(MBG)的使用 MBG可以根据数据表生成对应的model.xml映射文件.mapper接口,只是简单的生成,还需要根据需求修改. 1.下载jar包 https://github.com/mybatis/generator/releases 解压后有3个jar包,只使用一个: 2.新建一个新的java项目,导入mybatis.jar.mybatis-generator-core.jar.数据库驱动. 3.src下新建config.xml http://mybat

Python学习 Day17 Python对Mysql操作和使用ORM框架(SQLAlchemy)

Python对Mysql操作和使用ORM框架(SQLAlchemy) Mysql 常见操作 数据库操作 创建数据库 create database fuzjtest 删除数据库 drop database fuzjtest 查询数据库       show databases 切换数据库       use databas 123123 ###用户授权 创建用户          create user '用户名'@'IP地址' identified by '密码'; 删除用户        

mysql修改数据表自增步长

可以修改系统变量 auto_increment_increment mysql> SHOW VARIABLES LIKE 'auto_inc%'; +--------------------------+-------+ | Variable_name | Value | +--------------------------+-------+ | auto_increment_increment | 1 | //查看步长 | auto_increment_offset | 1 | +-----

python环境下使用mysql数据及数据结构和二叉树算法(图)

python环境下使用mysql数据及数据结构和二叉树算法(图):1 python环境下使用mysql2使用的是 pymysql库3 开始-->创建connection-->获取cursor-->操作-->关闭cursor->关闭connection->结束45 代码框架6 import pymysql.cursors7 ###连接数据库8 connection = pymysql.connect(host='127.0.0.1',port=3306,user='roo

MySQL数据备份 | 与python的交互 | MySQL

mysqldump -uroot -p 数据库名 > python.sql; # 按提示输入密码 连接mysql,创建新的数据库 退出连接,执行如下命令 mysql -uroot -p 新数据库名 < python.sql # 根据提示输入mysql密码 from pymysql import * def main(): # 获取要查询的商品名称 find_name = input("请输入要查询的商品类型名:") # 创建链接对象 conn = connect(host=

python SQLAlchemy

这里我们记录几个python SQLAlchemy的使用例子: 如何对一个字段进行自增操作 user = session.query(User).with_lockmode('update').get(1)user.age += 1session.commit() 对多条数据进行删除操作: session.query(User).filter(or_(User.id == 1, User.id == 2, User.id == 3)).delete() 各种连接表,并进行多表操作: from s

Python—sqlalchemy

SQLAlchemy SQLAlchemy是Python编程语言下的一款ORM框架,该框架建立在数据库API之上,使用关系对象映射进行数据库操作. #Dialect用于和数据API进行交流,根据配置文件的不同调用不同的数据库API,从而实现对数据库的操作: ''' MySQL-Python mysql+mysqldb://<user>:<password>@<host>[:<port>]/<dbname> pymysql mysql+pymysq

Python SqlAlchemy使用方法

Python SqlAlchemy使用方法 1.初始化连接 from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker engine = create_engine('mysql://[email protected]/test'echo=True) DBSession = sessionmaker(bind=engine) session = DBSession() ret=session.execu