配置自动生成sqlalchemy代码环境:
-
pip install sqlacodegen
-
pip install mysqlclient
-
pip install sqlalchemy
-
Python获取当前文件名的两种方法:https://blog.youkuaiyun.com/iamcodingmylife/article/details/79866600
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
- python cmd命令调用:https://www.cnblogs.com/lrw3716740/p/5158494.html
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 后缀 . . . . . . . :
- Sqlalchemy的model文件自动生成:https://www.cnblogs.com/timelesszhuang/p/8538929.html
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))
- 在桌面创建sqlacodegen的model代码:https://www.cnblogs.com/yanglang/p/7428884.html
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 TABLErole
(
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 TABLEuser
(
nid
int(11) NOT NULL AUTO_INCREMENT,
name
varchar(10) NOT NULL,
role
int(11) DEFAULT NULL,
PRIMARY KEY (nid
),
KEYrole
(role
),
CONSTRAINTuser_ibfk_1
FOREIGN KEY (role
) REFERENCESrole
(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')]