当前位置:网站首页>ORM 实现类与表,类属性与字段的映射关系
ORM 实现类与表,类属性与字段的映射关系
2022-06-12 21:23:00 【阿狸的情书】
# 类 -> 表
# 类属性 -> 字段
class model:
def __init__(self, max_length=100):
self.max_length = max_length
self.table = self.__class__.__name__
self.fields = [i for i in dir(self) if i not in ['save', 'table', 'max_length'] and i[0:2] != '__']
values = [getattr(self, i) for i in self.fields]
def save(self):
table = self.table
fields = self.fields
fields_sql = [f'`%s` varchar({
self.max_length}) not null,\n' % i for i in fields]
my_fields_sql = "".join(fields_sql)
sql = f'''create table if not exists `{
table}`(`id` int unsigned auto_increment,\n''' + my_fields_sql + 'primary key (`id`))engine=InnoDB default charset=utf8; '
print(sql)
class CharField(model):
pass
class Cat(model):
head = CharField(max_length=200)
foot = CharField()
Cat().save()
输出结果:
create table if not exists `Cat`(`id` int unsigned auto_increment,
`foot` varchar(100) not null,
`head` varchar(100) not null,
primary key (`id`))engine=InnoDB default charset=utf8;
边栏推荐
- Select sort
- Market trend report, technical innovation and market forecast of hydraulic torque wrench in China
- #141 Linked List Cycle
- 测试基础之:单元测试
- The required books for software testers (with e-books) recommended by senior Ali have benefited me a lot
- zgc的垃圾收集的主要階段
- Bluetooth for Delphi xe7
- 好数对的求解
- Ubuntu 16.04 installing mysql5.6
- Can tonghuashun open an account? Is it safe to open an account in tonghuashun
猜你喜欢

金融信创爆发年!袋鼠云数栈DTinsight全线产品通过信通院信创专项测试

竣达技术丨适用于“科士达”智能精密空调网络监控

New product release Junda intelligent integrated environmental monitoring terminal

Junda technology is applicable to "kestar" intelligent precision air conditioning network monitoring

Solve the cvxpy error the solver GLPK_ MI is not installed

Composer version degradation

Data visualization - histogram

Integrated monitoring solution for power environment of small and medium-sized computer rooms

Data visualization - biaxial comparison effect

EU officially released the data act, Ukraine was attacked by DDoS again, kitchen appliance giant Meiya was attacked, internal data leakage network security weekly
随机推荐
Compréhension préliminaire des expressions régulières cognitives (regex)
同花顺能开户吗,在同花顺开户安全么
Junda technology is applicable to "kestar" intelligent precision air conditioning network monitoring
To delete a character from a string
Research Report on market supply and demand and strategy of China's hydraulic injection molding machine industry
The service did not report any errors MySQL
Solve the cvxpy error the solver GLPK_ MI is not installed
Gather function in pytorch_
Lintcode:127. Topology sorting
Understanding of functions
实现从字符串中删除某个字符操作
USB mechanical keyboard changed to Bluetooth Keyboard
Bubble sort
Pytorch how to set random number seed to make the result repeatable
字符串基础知识
杨辉三角代码实现
[tutorial] detailed explanation of the page rules parameter settings of cloudflare CDN
Lake shore PT-100 platinum resistance temperature sensor
[tutorial] Firefox send: deployment method of Firefox open source temporary file sharing service platform
一级指针&二级指针知识点梳理