Python Django | 教程 | 6.数据模型类
数据模型类:概念与说明
在前面的Django内容中,讲到了通过python manage.py startapp xxx,可以给一个Django项目创建一个新的应用。
而每个Django的应用中,都包含一个名为models.py的文件,该文件就是应用的数据模型类,它用于定义当前应用的数据结构;该[……]
Adamhuan's Data Center - 【逻辑驱动数据】
数据玩物、代码屋、1/0游戏:(零和博弈)/ 禅宗意志 / 规则战争 / 解放数据力量 / 技术的飞速发展并没有改变这个世界,因为,这个世界从没有变,它只是越来越趋近于它本来的模样。
通过Ansible安装Archery,代码在百度网盘上:
链接:https://pan.baidu.com/s/1rULS1FXx8XERHvW9_4C_ug
提取码:0sjy
执行过程如下:
如上,最后在执行一个创建超级管理员用户的命令即可。
在上面的脚本跑完了以后,[……]
开始本文档之前,需要安装go语言支持,版本:1.14以上
在CentOS7.8上安装go支持可以参考这篇文档:
http://d-prototype.com/archives/15694
goInception的github官方网站: https://github.com/hanchuanchua[……]
SQL脚本:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
[root@rhcs2 sql]# pwd /script/sql [root@rhcs2 sql]# [root@rhcs2 sql]# ls -ltr total 4 -rw-r--r-- 1 root root 2558 Oct 19 11:05 give_me_big_data.sql [root@rhcs2 sql]# [root@rhcs2 sql]# cat give_me_big_data.sql DROP TABLE IF EXISTS `vote_record_memory`; CREATE TABLE `vote_record_memory` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `user_id` varchar(20) NOT NULL DEFAULT '', `vote_num` int(10) unsigned NOT NULL DEFAULT '0', `group_id` int(10) unsigned NOT NULL DEFAULT '0', `status` tinyint(2) unsigned NOT NULL DEFAULT '1', `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`id`), KEY `index_user_id` (`user_id`) USING HASH ) ENGINE=MEMORY AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; DROP TABLE IF EXISTS `vote_record`; CREATE TABLE `vote_record` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `user_id` varchar(20) NOT NULL DEFAULT '' COMMENT '用户Id', `vote_num` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '投票数', `group_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '用户组id 0-未激活用户 1-普通用户 2-vip用户 3-管理员用户', `status` tinyint(2) unsigned NOT NULL DEFAULT '1' COMMENT '状态 1-正常 2-已删除', `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`id`), KEY `index_user_id` (`user_id`) USING HASH COMMENT '用户ID哈希索引' ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='投票记录表'; -- 创建生成长度为n的随机字符串的函数 DELIMITER // -- 修改MySQL delimiter:'//' DROP FUNCTION IF EXISTS `rand_string` // SET NAMES utf8 // CREATE FUNCTION `rand_string` (n INT) RETURNS VARCHAR(255) CHARSET 'utf8' BEGIN DECLARE char_str varchar(100) DEFAULT 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; DECLARE return_str varchar(255) DEFAULT ''; DECLARE i INT DEFAULT 0; WHILE i < n DO SET return_str = concat(return_str, substring(char_str, FLOOR(1 + RAND()*62), 1)); SET i = i+1; END WHILE; RETURN return_str; END // -- 创建插入数据的存储过程 DROP PROCEDURE IF EXISTS `add_vote_record_memory` // CREATE PROCEDURE `add_vote_record_memory`(IN n INT) BEGIN DECLARE i INT DEFAULT 1; DECLARE vote_num INT DEFAULT 0; DECLARE group_id INT DEFAULT 0; DECLARE status TINYINT DEFAULT 1; WHILE i < n DO SET vote_num = FLOOR(1 + RAND() * 10000); SET group_id = FLOOR(0 + RAND()*3); SET status = FLOOR(1 + RAND()*2); INSERT INTO `vote_record_memory` VALUES (NULL, rand_string(20), vote_num, group_id, status, NOW()); SET i = i + 1; END WHILE; END // DELIMITER ; -- 改回默认的 MySQL delimiter:';' [root@rhcs2 sql]# |
MySQL,创建数据库,并执行SQL:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
[root@rhcs2 sql]# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 9 Server version: 5.7.19-17 Percona Server (GPL), Release 17, Revision e19a6b7b73f Copyright (c) 2009-2017 Percona LLC and/or its affiliates Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec) mysql> mysql> create database you; Query OK, 1 row affected (0.00 sec) mysql> mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | you | +--------------------+ 5 rows in set (0.00 sec) mysql> mysql> select database(); +------------+ | database() | +------------+ | NULL | +------------+ 1 row in set (0.00 sec) mysql> mysql> use you; Database changed mysql> mysql> select database(); +------------+ | database() | +------------+ | you | +------------+ 1 row in set (0.00 sec) mysql> mysql> show tables; Empty set (0.00 sec) mysql> mysql> source /script/sql/give_me_big_data.sql Query OK, 0 rows affected, 1 warning (0.00 sec) Query OK, 0 rows affected (0.43 sec) Query OK, 0 rows affected, 1 warning (0.00 sec) Query OK, 0 rows affected (0.48 sec) Query OK, 0 rows affected, 1 warning (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected (0.00 sec) Query OK, 0 rows affected, 1 warning (0.00 sec) Query OK, 0 rows affected (0.00 sec) mysql> mysql> show tables; +--------------------+ | Tables_in_you | +--------------------+ | vote_record | | vote_record_memory | +--------------------+ 2 rows in set (0.00 sec) mysql> mysql> desc vote_record; +-------------+---------------------+------+-----+-------------------+-----------------------------+ | Field | Type | Null | Key | Default | Extra | +-------------+---------------------+------+-----+-------------------+-----------------------------+ | id | int(10) unsigned | NO | PRI | NULL | auto_increment | | user_id | varchar(20) | NO | MUL | | | | vote_num | int(10) unsigned | NO | | 0 | | | group_id | int(10) unsigned | NO | | 0 | | | status | tinyint(2) unsigned | NO | | 1 | | | create_time | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP | +-------------+---------------------+------+-----+-------------------+-----------------------------+ 6 rows in set (0.00 sec) mysql> mysql> desc vote_record_memory; +-------------+---------------------+------+-----+-------------------+-----------------------------+ | Field | Type | Null | Key | Default | Extra | +-------------+---------------------+------+-----+-------------------+-----------------------------+ | id | int(10) unsigned | NO | PRI | NULL | auto_increment | | user_id | varchar(20) | NO | MUL | | | | vote_num | int(10) unsigned | NO | | 0 | | | group_id | int(10) unsigned | NO | | 0 | | | status | tinyint(2) unsigned | NO | | 1 | | | create_time | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP | +-------------+---------------------+------+-----+-------------------+-----------------------------+ 6 rows in set (0.00 sec) mysql> mysql> select * from vote_record; Empty set (0.00 sec) mysql> mysql> select * from vote_record_memory; Empty set (0.00 sec) mysql> |
执行存储过程,生成数据【1K条】:
[crayon-63d42072cb6771445055[……]