python 從資料庫表生成model

来源:http://www.cnblogs.com/freegodly/archive/2016/01/15/5132243.html
-Advertisement-
Play Games

python 從資料庫表生成model找了很久才找到這個,我是新手...現在已有建好的資料庫,需要基於原有數據做數據分析的web應用,我選擇python+Tornado,由於不想寫SQL語句,就想偷個懶1、安裝工具1 ningjian@freegodly:~/code/py/django/logcl...


python 從資料庫表生成model

 

找了很久才找到這個,我是新手...

現在已有建好的資料庫,需要基於原有數據做數據分析的web應用,我選擇python+Tornado ,由於不想寫SQL語句,就想偷個懶

 

1、安裝工具

1 ningjian@freegodly:~/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、轉換

ningjian@freegodly:~/code/py/django/logcloud$ sqlacodegen mssql+pymssql://name:password@ip/LogColudDB --outfile logcloude_model.py
ningjian@freegodly:~/code/py/django/logcloud$

 

3、查看,哈哈

ningjian@freegodly:~/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)
)
ningjian@freegodly:~/code/py/django/logcloud$

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 流程式控制制 1.If,then,else,elsif(不是elseif) if a='1' then null; endif; 2.Case 簡單case表達式: 搜索型Case表達式: 3.goto語句 begin if true then goto label2; end if; > SYS.DB...
  • 剛逛論壇,發現一個這樣的問題,如果不建立一個新的月份的表,可以用CET來解決。給定一張表(列有月份,銷售額),要求查詢出月份、本月銷售額、上月銷售額這三個結果,如果當月上個月的銷售額不存在就顯示為“*”。if exists (select * from sysobjects where id = o...
  • 工作和學習中常常會遇到一行要分割成多行數據的情況,在此整理一下做下對比。 單行拆分 如果表數據只有一行,則可以直接在原表上直接使用connect by+正則的方法,比如: select regexp_substr('444.555.666', '[^.]+', 1, level) col from ...
  • 前言 大家應該都知道Memcached要想實現分散式只能在客戶端來完成,目前比較流行的是通過一致性hash演算法來實現.常規的方法是將server的hash值與server的總台數進行求餘,即hash%N,這種方法的弊端是當增減伺服器時,將會有較多的緩存需要被重新分配且會造成緩存分配不均勻的情況(有....
  • 1. Get(即使用QueryString顯式傳遞) 方式:在url後面跟參數。 特點:簡單、方便。 缺點:字元串長度最長為255個字元;數據泄漏在url中。 適用數據:簡單、少量、關鍵的數據。 適用範圍:傳遞給自己、傳遞給另一個目標頁面;常用於2個頁面間傳遞數據。 用法:例如:url後加?User...
  • 自網站誕生以來,響應速度/響應時間一直都是大家關心的話題,而速度慢乃是網站的一個殺手,正當大家以為四核和寬頻能力的提升能夠解決這些問題時,Wi-Fi和移動設備為熱點移動互聯網又悄然興起。 在2006年,Amazon曾做過一個報道,響應時間每提高100ms,他們便會增加1%的收入。優化的價值已顯而易....
  • 解決方案中有一個 Project 是 Windows Service,用來從消息隊列中取出事件,發送通知電郵;UI是一個MVC網站,兩個Project都引用了同一個類庫,這個類庫引用了第三方的生成PDF組件 Pechkin;我每次生成網站時,Pechkin的7個DLL都會自動拷貝到 bin 目錄,7...
  • Orchard是支持多種資料庫的如果是個人站長推薦使用MySql作為運行資料庫,雖然SqlServer更為強大,但總覺得SqlServer好重啊,一裝就是幾個G。最近的版本在使用MySql建庫時卻會出現,http://yjx.in/System.Reflection.TargetInvocation...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...