Python利用sqlacodegen自动生成ORM实体类示例


Posted in Python onJune 04, 2019

本文实例讲述了Python利用sqlacodegen自动生成ORM实体类。分享给大家供大家参考,具体如下:

在前面一篇《Python流行ORM框架sqlalchemy安装与使用》我们是手动创建了一个名叫Infos.py的文件,然后定义了一个News类,把这个类作为和我们news数据表的映射。

from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
from sqlalchemy import Column, Integer, String
class News(Base):
  # 表名称
  __tablename__ = 'news'
  # news表里id字段
  id = Column(Integer, primary_key=True, autoincrement=True)
  # news表里title字段
  title = Column(String(length=255), nullable=False)

现在我们来看看sqlacodegen这个工具,自动生成像上面那样的类文件。

1、安装sqlacodegen

#cd 项目虚拟环境
#执行
./python3 -m pip install sqlacodegen

2、使用sqlacodegen生成案列

#注意还是在虚拟环境目录下执行
./sqlacodegen --tables fund --outfile ../../mappers/Found.py mysql+pymysql://root:root@localhost/test?charset=utf8

--tables指定数据表名称,我们给fund基金数据表生成。
--outfile指定输出文件名称。

3、生成的Fund.py文件代码如下:

# coding: utf-8
from sqlalchemy import Column, DateTime, Numeric, String
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
metadata = Base.metadata
class Fund(Base):
  __tablename__ = 'fund'
  code = Column(String(50), primary_key=True)
  name = Column(String(255))
  NAV = Column(Numeric(5, 4))
  ACCNAV = Column(Numeric(5, 4))
  updated_at = Column(DateTime)

这样就不用手动写啦。

希望本文所述对大家Python程序设计有所帮助。

Python 相关文章推荐
用Python中的__slots__缓存资源以节省内存开销的方法
Apr 02 Python
Python中内置数据类型list,tuple,dict,set的区别和用法
Dec 14 Python
Python实现的计数排序算法示例
Nov 29 Python
Python使用pip安装报错:is not a supported wheel on this platform的解决方法
Jan 23 Python
Python迭代器与生成器基本用法分析
Jul 26 Python
超简单使用Python换脸实例
Mar 27 Python
python操作excel让工作自动化
Aug 09 Python
Anaconda+VSCode配置tensorflow开发环境的教程详解
Mar 30 Python
jupyter notebook 重装教程
Apr 16 Python
Python创建文件夹与文件的快捷方法
Dec 08 Python
Python实现简单的猜单词
Jun 15 Python
Python Matplotlib绘制等高线图与渐变色扇形图
Apr 14 Python
Python批量生成幻影坦克图片实例代码
Jun 04 #Python
python和mysql交互操作实例详解【基于pymysql库】
Jun 04 #Python
Python获取基金网站网页内容、使用BeautifulSoup库分析html操作示例
Jun 04 #Python
Python使用MyQR制作专属动态彩色二维码功能
Jun 04 #Python
Python流行ORM框架sqlalchemy安装与使用教程
Jun 04 #Python
Python3日期与时间戳转换的几种方法详解
Jun 04 #Python
Falsk 与 Django 过滤器的使用与区别详解
Jun 04 #Python
You might like
PHP实现返回JSON和XML的类分享
2015/01/28 PHP
php socket通信(tcp/udp)实例分析
2016/02/14 PHP
php菜单/评论数据递归分级算法的实现方法
2019/08/01 PHP
laravel 出现command not found问题的解决方案
2019/10/23 PHP
Jquery选择器 $实现原理
2009/12/02 Javascript
Whatever:hover 无需javascript让IE支持丰富伪类
2010/06/29 Javascript
js判断输入是否为正整数、浮点数等数字的函数代码
2010/11/17 Javascript
20款非常优秀的 jQuery 工具提示插件 推荐
2012/07/15 Javascript
javascript父、子页面交互技巧总结
2014/08/08 Javascript
jQuery提示插件alertify使用指南
2015/04/21 Javascript
JS中用三种方式实现导航菜单中的二级下拉菜单
2016/10/31 Javascript
JavaScript中浅讲ajax图文详解
2016/11/11 Javascript
深入理解vue.js中的v-if和v-show
2017/06/22 Javascript
React Native中Navigator的使用方法示例
2017/10/13 Javascript
浅谈webpack打包生成的bundle.js文件过大的问题
2018/02/22 Javascript
NodeJS如何实现同步的方法示例
2018/08/24 NodeJs
微信小程序开发之自定义tabBar的实现
2018/09/06 Javascript
[06:42]DOTA2每周TOP10 精彩击杀集锦vol.1
2014/06/25 DOTA
Linux 下 Python 实现按任意键退出的实现方法
2016/09/25 Python
python+pillow绘制矩阵盖尔圆简单实例
2018/01/16 Python
Python Unittest自动化单元测试框架详解
2018/04/04 Python
python 反向输出字符串的方法
2018/07/16 Python
Pandas之Dropna滤除缺失数据的实现方法
2019/06/25 Python
Win10系统下安装labelme及json文件批量转化方法
2019/07/30 Python
Pytorch: 自定义网络层实例
2020/01/07 Python
tensorflow保持每次训练结果一致的简单实现
2020/02/17 Python
Windows 下更改 jupyterlab 默认启动位置的教程详解
2020/05/18 Python
python开发前景如何
2020/06/11 Python
HTML5单页面手势滑屏切换原理
2016/03/21 HTML / CSS
保荐人的岗位职责
2013/11/19 职场文书
生日寿宴答谢词
2014/01/19 职场文书
七一党建活动方案
2014/01/28 职场文书
致百米运动员广播稿
2014/01/29 职场文书
放假通知格式
2015/04/14 职场文书
学习心得体会
2019/06/20 职场文书
nginx配置虚拟主机的详细步骤
2021/07/21 Servers