使用sqlacodegen自动生成sqlalchemy的model代码

配置自动生成sqlalchemy代码环境:

import os, sys
# 该python文件本身的路径(全路径)
print(sys.argv[0])  # 在Jupyter notebook中不能获得真正的路径
# 当前文件名名称
print(os.path.basename(sys.argv[0]))  # 在Jupyter notebook中不能获得真正的文件名称
d:\program files\python36\lib\site-packages\ipykernel_launcher.py
ipykernel_launcher.py
import os
# 只返回一个数字,表示是否正常运行指令
ret = os.system("ipconfig")
print(ret)
# 带返回值
ret = os.popen("ipconfig")
print(ret.read())
0

Windows IP 配置


以太网适配器 以太网:

   媒体状态  . . . . . . . . . . . . : 媒体已断开连接
   连接特定的 DNS 后缀 . . . . . . . : ncu.edu.cn

无线局域网适配器 本地连接* 3:

   媒体状态  . . . . . . . . . . . . : 媒体已断开连接
   连接特定的 DNS 后缀 . . . . . . . : 

无线局域网适配器 本地连接* 12:

   媒体状态  . . . . . . . . . . . . : 媒体已断开连接
   连接特定的 DNS 后缀 . . . . . . . : 

以太网适配器 VMware Network Adapter VMnet1:

   连接特定的 DNS 后缀 . . . . . . . : 
   本地链接 IPv6 地址. . . . . . . . : xxxx::xxxx:xxxx:xxxx:xxxx%9
   IPv4 地址 . . . . . . . . . . . . : xxx.xxx.xxx.1
   子网掩码  . . . . . . . . . . . . : 255.255.255.0
   默认网关. . . . . . . . . . . . . : 

以太网适配器 VMware Network Adapter VMnet8:

   连接特定的 DNS 后缀 . . . . . . . : 
   本地链接 IPv6 地址. . . . . . . . : xxxx::xxxx:xxxx:xxxx:xxxx%9
   自动配置 IPv4 地址  . . . . . . . : xxx.xxx.xx.122
   子网掩码  . . . . . . . . . . . . : 255.255.0.0
   默认网关. . . . . . . . . . . . . : 

无线局域网适配器 WLAN:

   连接特定的 DNS 后缀 . . . . . . . : 
   本地链接 IPv6 地址. . . . . . . . :  xxxx::xxxx:xxxx:xxxx:xxxx%9
   IPv4 地址 . . . . . . . . . . . . : xxx.xxx.xx.16
   子网掩码  . . . . . . . . . . . . : 255.255.255.0
   默认网关. . . . . . . . . . . . . : xxx.xxx.xx.1

以太网适配器 蓝牙网络连接:

   媒体状态  . . . . . . . . . . . . : 媒体已断开连接
   连接特定的 DNS 后缀 . . . . . . . : 
import os
ret = os.popen(
    "sqlacodegen --noviews --noconstraints --noindexes mysql://root:root@127.0.0.1:3306/test"
)
print(ret.read())
# coding: utf-8
from sqlalchemy import Column, String
from sqlalchemy.dialects.mysql import INTEGER
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()
metadata = Base.metadata


class Role(Base):
    __tablename__ = 'role'

    rid = Column(INTEGER(11), primary_key=True)
    role_name = Column(String(10))


class User(Base):
    __tablename__ = 'user'

    nid = Column(INTEGER(11), primary_key=True)
    name = Column(String(10), nullable=False)
    role = Column(INTEGER(11))
import os
ret = os.popen(
    "sqlacodegen --noviews --noconstraints --noindexes --outfile C:\\Users\\Administrator\\Desktop\\models.py mysql://root:root@127.0.0.1:3306/test"
)
print(ret)
<os._wrap_close object at 0x0000024ADD68A518>

自动生成sqlalchemy实操

表结构

  • 角色表结构
    CREATE TABLE role (
    rid int(11) NOT NULL AUTO_INCREMENT,
    role_name varchar(10) DEFAULT NULL,
    PRIMARY KEY (rid)
    ) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8;

  • 用户表
    CREATE TABLE user (
    nid int(11) NOT NULL AUTO_INCREMENT,
    name varchar(10) NOT NULL,
    role int(11) DEFAULT NULL,
    PRIMARY KEY (nid),
    KEY role (role),
    CONSTRAINT user_ibfk_1 FOREIGN KEY (role) REFERENCES role (rid)
    ) ENGINE=InnoDB AUTO_INCREMENT=19 DEFAULT CHARSET=utf8;

实操来了

  • 在当前目录下的auto_file文件中自动创建sqlalchemy的model代码
import os
os.system(
    "sqlacodegen --noviews --noconstraints --noindexes --outfile ./auto_file/models.py mysql://root:root@127.0.0.1:3306/test"
)
0
  • 将models.py文件导入导当前文件中
# %load ./auto_file/models.py
from sqlalchemy import Column, String
from sqlalchemy.dialects.mysql import INTEGER
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()
metadata = Base.metadata


class Role(Base):
    __tablename__ = 'role'

    rid = Column(INTEGER(11), primary_key=True)
    role_name = Column(String(10))


class User(Base):
    __tablename__ = 'user'

    nid = Column(INTEGER(11), primary_key=True)
    name = Column(String(10), nullable=False)
    role = Column(INTEGER(11))
  • 添加角色数据
from sqlalchemy.orm import sessionmaker, relationship
from sqlalchemy import create_engine
engine = create_engine(
    "mysql+pymysql://root:root@127.0.0.1/test",
    encoding="utf-8",
    #echo=True,
    max_overflow=5)

Session = sessionmaker(bind=engine)
session = Session()

#添加角色数据
session.add(Role(role_name='dba'))
session.add(Role(role_name='sa'))
session.add(Role(role_name='net'))

session.commit()
session.close()
  • 添加用户数据
from sqlalchemy.orm import sessionmaker, relationship
from sqlalchemy import create_engine

engine = create_engine(
    "mysql+pymysql://root:root@127.0.0.1/test",
    encoding="utf-8",
    #echo=True,
    max_overflow=5)

Session = sessionmaker(bind=engine)
session = Session()

#添加用户数据
session.add_all([
    User(name='fuzj', role='1'),
    User(name='jie', role='2'),
    User(name='张三', role='2'),
    User(name='李四', role='1'),
    User(name='王五', role='3'),
])

session.commit()
session.close()
  • 普通连表查询
from sqlalchemy.orm import sessionmaker, relationship
from sqlalchemy import create_engine
engine = create_engine(
    "mysql+pymysql://root:root@127.0.0.1/test",
    encoding="utf-8",
    #echo=True,
    max_overflow=5)

Session = sessionmaker(bind=engine)
session = Session()

res = session.query(User,Role).join(Role).all()    #查询所有用户,及对应的role id
res1 = session.query(User.name,Role.role_name).join(Role).all()  #查询所有用户和角色,
res2 = session.query(User.name,Role.role_name).join(Role,isouter=True).filter(Role.role_name=='sa').all() #查询所有DBA的用户

print(res)
print(res1)
print(res2)
[((9,fuzj,1), (1,dba)), ((12,李四,1), (1,dba)), ((14,fuzj,1), (1,dba)), ((17,李四,1), (1,dba)), ((10,jie,2), (2,sa)), ((11,张三,2), (2,sa)), ((15,jie,2), (2,sa)), ((16,张三,2), (2,sa)), ((13,王五,3), (3,net)), ((18,王五,3), (3,net))]
[('fuzj', 'dba'), ('李四', 'dba'), ('fuzj', 'dba'), ('李四', 'dba'), ('jie', 'sa'), ('张三', 'sa'), ('jie', 'sa'), ('张三', 'sa'), ('王五', 'net'), ('王五', 'net')]
[('jie', 'sa'), ('张三', 'sa'), ('jie', 'sa'), ('张三', 'sa')]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值