- 把网站需要的表用Model表示出来(www下的models.py):
import time, uuid
from orm import Model, StringField, BooleanField, FloatField, TextField
def next_id():
# %015d表示15个占位符0
# uuid4()函数基于随机数设置唯一值, hex()函数将一个整数转换为十六进制的字符串
return '%015d%s000' % (int(time.time() * 1000), uuid.uuid4().hex)
class User(Model):
__table__ = 'users'
# id缺省值是函数next_id(), # created_at缺省值是函数time.time()
id = StringField(primary_key=True, default=next_id, ddl='varchar(50)')
email = StringField(ddl='varchar(50)')
passwd = StringField(ddl='varchar(50)')
admin = BooleanField()
name = StringField(ddl='varchar(50)')
image = StringField(ddl='varchar(500)')
created_at = FloatField(default=time.time)
class Blog(Model):
__table__ = 'blogs'
id = StringField(primary_key=True, default=next_id, ddl='varchat(50)')
user_id = StringField(ddl='varchar(50)')
user_name = StringField(ddl='varchar(50)')
user_image = StringField(ddl='varchar(500)')
name = StringField(ddl='varchar(50)')
summary = StringField(ddl='varchar(200)')
content = TextField()
created_at = FloatField(default=time.time)
class Comment(Model):
__table__ = 'comments'
id = StringField(primary_key=True, default=next_id, ddl='varchar(50)')
blog_id = StringField(ddl='varchar(50)')
user_id = StringField(ddl='varchar(50)')
user_name = StringField(ddl='varchar(50)')
user_image = StringField(ddl='varchar(500)')
content = TextField()
created_at = FloatField(default=time.time)
- 创建表的SQL脚本:
drop database if exists awesome;
create database awesome;
use awesome;
# 将awesome数据库下所有表的增删改查权限授予本地用户'www-data', 密码为'www-data'
grant select, insert, update, delete on awesome.* to 'www-data'@'localhost' identified by 'www-data';
create table users (
`id` varchar(50) not null,
`email` varchar(50) not null,
`passwd` varchar(50) not null,
`admin` bool not null,
`name` varchar(50) not null,
`image` varchar(500) not null,
`created_at` real not null,
# 在email列创建UNIQUE约束,约束名为idx_email
unique key `idx_email` (`email`),
# 为created_at字段创建索引,加快查询速度
key `idx_created_at` (`created_at`),
primary key (`id`)
) engine=innodb default charset=utf8;
create table blogs (
`id` varchar(50) not null,
`user_id` varchar(50) not null,
`user_name` varchar(50) not null,
`user_image` varchar(500) not null,
`name` varchar(50) not null,
`summary` varchar(200) not null,
`content` mediumtext not null,
`created_at` real not null,
key `idx_created_at` (`created_at`),
primary key (`id`)
) engine=innodb default charset=utf8;
create table comments (
`id` varchar(50) not null,
`blog_id` varchar(50) not null,
`user_id` varchar(50) not null,
`user_name` varchar(50) not null,
`user_image` varchar(500) not null,
`content` mediumtext not null,
`created_at` real not null,
key `idx_created_at` (`created_at`),
primary key (`id`)
) engine=innodb default charset=utf8;
把SQL脚本放到MySQL命令行里执行,完成表的初始化:
$ mysql -u root -p < schema.sql
- 写个例子测试一下(www下test.py):
import orm
from models import User, Blog, Comment
import asyncio
async def test(loop):
await orm.create_pool(loop=loop, user='www-data', password='www-data', db='awesome')
u = User(name='Test', email='test@example.com', passwd='1234', image='about:blank')
await u.save()
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(test(loop))
loop.close()
运行test.py,再到数据库里看一下是不是成功添加好了:
mysql> select * from users;
+----------------------------------------------------+------------------+--------+-------+------+-------------+------------------+
| id | email | passwd | admin | name | image | created_at |
+----------------------------------------------------+------------------+--------+-------+------+-------------+------------------+
| 001510302101523005ebbe6e6f740c7b3e95627004ec515000 | test@example.com | 1234 | 0 | Test | about:blank | 1510302101.52332 |
| 1 | 123 | lSER | 0 | LX | about:blank | 2017 |
+----------------------------------------------------+------------------+--------+-------+------+-------------+------------------+
2 rows in set (0.00 sec)
没有问题~