#/usr/bin/python
#-*- coding:utf-8 -*-
#@Time   :2017/11/24 2:59
#@Auther :liuzhenchuan
#@File   :增加#数据.py
 
#select * from tmp     10行数据
#select * from tmp a ,tmp b, tmp c:   1000行数据=10*10*10
#tmp a   tmp b   tmp c   对应表1  表2  表3
 
#获得随机字符串的设计
#增加的数据是随机数据,rand()  函数生成0-1 的一个随机数
#shal()    对数字进行加密,然后就生成了一堆字符串
#concat()   拼接多个字符串的函数
#substr()    取多少个字符
 
#获得随机整数的设计
# rand()*50  通过rand() 函数获得一个0-1的数,然后再乘以50 取到的数就是0-50
# floor()  这个函数代表的是去尾法取整数
 
# 男女的设计
# rand()*10 /2  最后取余数
# 如果余数为1,就设置为M
#如果余数为0,就设置为F
 
import  MySQLdb
student = '''
    set @a = 10000;
    insert into student select @a := @a + 1, substr(concat(sha1(rand()), sha1(rand())), 1, 5+floor(rand()* 50)), case floor(rand()*10) mod 2 when 1 then "M" else "F" end, 20+floor(rand() * 8) from tmp a, tmp b, tmp c, tmp d;
'''
# insert into student select @i:=@i+1, substr(concat(sha1(rand()), sha1(rand())), 1, 3 + floor(rand() * 75)), case floor(rand()*10) mod 2 when 1 then 'M' else 'F' end, 25-floor(rand() * 5)  from tmp a, tmp b, tmp c, tmp d;
# 我用下去这种insert就会有同名字的学生名
 
course = '''set @i := 10;
            insert into course select @i:=@i+1, substr(concat(sha1(rand()), sha1(rand())), 1, 5 + floor(rand() * 45)),  100 + floor(rand() * 100) from tmp a;
'''
 
score = '''set @i := 10000;
            insert into score select @i := @i +1, floor(10001 + rand()*10000), floor(11 + rand()*10), floor(1+rand()*100) from tmp a, tmp b, tmp c, tmp d;
'''
teacher = '''set @i := 100;
           insert into teacher select @i:=@i+1, substr(concat(sha1(rand()), sha1(rand())), 1, 5 + floor(rand() * 80)) from tmp a, tmp b;
'''
 
 
def connect_mysql():
    db_config = {
        "host": "192.168.16.70",
        "port": 3306,
        "user": "root",
        "passwd": "123123",
        "db": "students",
        "charset": "utf8"
    }
    try:
        cnx = MySQLdb.connect(**db_config)
    except Exception as e:
        raise e
    return cnx
 
 
if __name__ == "__main__":
    cnx = connect_mysql()
    try:
        cus = cnx.cursor()
        cus.execute(student)
        cus.close()
 
        cus = cnx.cursor()
        cus.execute(course)
        cus.close()
 
        cus = cnx.cursor()
        cus.execute(score)
        cus.close()
 
        cus = cnx.cursor()
        cus.execute(teacher)
        cus.close()
 
        cnx.commit()
    except Exception as e:
        cnx.rollback()
        raise e
    finally:
        cnx.close()
 
 
查看数据库:
 
ysql> show tables;
+--------------------+
| Tables_in_students |
+--------------------+
| course             |
| score              |
| student            |
| teacher            |
| tmp                |
+--------------------+
mysql> select *from course limit 10;
+-------+---------------------------------------------+-----+
| couid | cname                                       | tid |
+-------+---------------------------------------------+-----+
|    11 | 86c85fe76342ff5e376264621                   | 100 |
|    12 | ccef65aa5b377ac39ddd7dc44                   | 143 |
|    13 | 0e8a391f36                                  | 146 |
|    14 | 6755d9fa73                                  | 146 |
|    15 | 8c79d4684d88065b1cecbe76                    | 143 |
|    16 | 6bf78ad3dd4f5db7751fcfff6e661d526e814c54    | 166 |
|    17 | 2210cf9debd4a096373fd452d057f5c0f           | 141 |
|    18 | 16e99f932a599ca8f940eeabebc04996c           | 126 |
|    19 | a839d37dffd0cdd80bc7d                       | 181 |
|    20 | 17601ade7e74aa81f3fdf23d47af85bf2e8ed232288 | 152 |
+-------+---------------------------------------------+-----+
10 rows in set (0.00 sec)
mysql> select *from score limit 10;
+-------+-------+-----+-------+
| sid   | stdid | cid | grade |
+-------+-------+-----+-------+
| 10001 | 10742 |  19 |    78 |
| 10002 | 14902 |  12 |    12 |
| 10003 | 12126 |  18 |    99 |
| 10004 | 17672 |  19 |     5 |
| 10005 | 16303 |  11 |    15 |
| 10006 | 17104 |  12 |    43 |
| 10007 | 18091 |  18 |    37 |
| 10008 | 15446 |  17 |    53 |
| 10009 | 17175 |  11 |    96 |
| 10010 | 16939 |  17 |   100 |
+-------+-------+-----+-------+
10 rows in set (0.00 sec)
mysql> select *from student limit 10;
+-------+-------------------------------------------------------+--------+-------+
| stdid | stdname                                               | gender | agent |
+-------+-------------------------------------------------------+--------+-------+
| 10001 | 05be2e1f6be2dba3496d                                  | M      |    27 |
| 10002 | 8d21176c8a4cad420d9adec8dd6022146ed0b69dabe80c56c91   | F      |    24 |
| 10003 | 582cf648fbb65fcae2eb0                                 | F      |    22 |
| 10004 | ece0f0bb303e98859d9d41b6f246864e22d86ad405048ee       | M      |    23 |
| 10005 | e484bfbd4011fcebfa0d18271975b0834a1ccedd              | M      |    26 |
| 10006 | bb1ecba374c1dbdda1be                                  | F      |    22 |
| 10007 | 0b30cf54b756d4665e8fc41c7450433f590aefc37c8ea9afbbe8e | M      |    27 |
| 10008 | 3cdf0dddcd8911386fd36b2bf3                            | F      |    27 |
| 10009 | f699c14f237ad1778fe2f1d                               | F      |    20 |
| 10010 | a4b36903fd1c0e2f8f4151706e9d95ebfdf068b2f             | F      |    27 |
+-------+-------------------------------------------------------+--------+-------+
10 rows in set (0.00 sec)
mysql> select *from teacher limit 10;
+-----+------------------------------------------------------------------------+
| tid | tname                                                                  |
+-----+------------------------------------------------------------------------+
| 101 | 7684b4b                                                                |
| 102 | 497c1eeb229d5faf8a88213f1a28ad5ceab755e222b832f50ef3b71f73860          |
| 103 | f7307                                                                  |
| 104 | 9c44341e4fd84d1545f5293df4762695e962540b39ebb528d58c509ab5             |
| 105 | d1cd631d236e48a8efccb032c833f867e863bbe361ef8cfd0869601                |
| 106 | 2cf3b9da2484eb560a8000be460f529dd17c                                   |
| 107 | edc0ad15105a4dde02214a482fd0ac695517e307999fb7695bf1f6cfead6           |
| 108 | 8109966982f40ad1819d9c94023f4227eb019bd779305c0b8e9a87a7b0ddd436e97842 |
| 109 | 76433                                                                  |
| 110 | 438032fb74888de54a09e2cbd846650c85079f8feda82                          |
+-----+------------------------------------------------------------------------+
10 rows in set (0.00 sec)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

pthon 基础 9.8 增加数据的更多相关文章

  1. [更新]跨平台物联网通讯框架 ServerSuperIO v1.2(SSIO),增加数据分发控制模式

    1.[开源]C#跨平台物联网通讯框架ServerSuperIO(SSIO) 2.应用SuperIO(SIO)和开源跨平台物联网框架ServerSuperIO(SSIO)构建系统的整体方案 3.C#工业 ...

  2. 计算概论(A)/基础编程练习(数据成分)/3:整数的个数

    #include<stdio.h> int main() { ] = {}; // 输入k个正整数 scanf("%d",&k); // 循环读入和进行算术 w ...

  3. 计算概论(A)/基础编程练习(数据成分)/2:奥运奖牌计数

    #include<stdio.h> int main() { // n天的决赛项目 int n; scanf("%d",&n); ] = {}; while ( ...

  4. 计算概论(A)/基础编程练习(数据成分)/1:短信计费

    #include<stdio.h> int main() { // 输入当月发送短信的总次数n和每次短信的字数words int n,words; scanf("%d" ...

  5. 多媒体基础知识之PCM数据《 转》

    多媒体基础知识之PCM数据 1.什么是PCM音频数据 PCM(Pulse Code Modulation)也被称为脉冲编码调制.PCM音频数据是未经压缩的音频采样数据裸流,它是由模拟信号经过采样.量化 ...

  6. Java基础-线程操作共享数据的安全问题

    Java基础-线程操作共享数据的安全问题 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.引发线程安全问题 如果有多个线程在同时运行,而这些线程可能会同时运行这段代码.程序每次运 ...

  7. ORACLE数据库增加表空间大小或给表空间增加数据文件

    转载 2017年11月24日 11:00:28 ----查询表空间使用情况--- SELECT UPPER(F.TABLESPACE_NAME) "表空间名", D.TOT_GRO ...

  8. laravel基础课程---16、数据迁移(数据库迁移是什么)

    laravel基础课程---16.数据迁移(数据库迁移是什么) 一.总结 一句话总结: 是什么:数据库迁移就像是[数据库的版本控制],可以让你的团队轻松修改并共享应用程序的数据库结构. 使用场景:解决 ...

  9. Django多变关联、增加数据、删除数据

    建立表之间的关联关系: models.py里面对表的字段及外键关系的设置如下: from django.db import models # Create your models here. #出版社 ...

随机推荐

  1. [Android Traffic] 根据网络类型更改下载模式

    转载自: http://blog.csdn.net/kesenhoo/article/details/7396321 Modifying your Download Patterns Based on ...

  2. [Debug] Diagnose a Slow Page Using Developer Tools

    Your page is showing high CPU usage and spinning up your laptop fan, but nothing is visibly happenin ...

  3. 【MVC5】First AngularJS

    ※本文参照<ASP.NET MVC 5高级编程(第5版)> 1.创建Web工程 1-1.选择ASP.NET Web Application→Web API 工程名为[atTheMovie] ...

  4. react-native + teaset 实现 Tabbar

    1.代码 src/pages/MainPage/index.js /** * 主页面 */ import React, {Component} from 'react'; import { BackH ...

  5. 小猪的Git使用总结

    小猪的Git使用总结 文件夹 小猪的Git使用总结 安装配置与文档 下载安装 文档教程相关 概念 Git的四个组成部分 文件的几个状态 Git与SVN版本号版本号控制存储差异 每次Commit时仓库中 ...

  6. poj 1390 Blocks (经典区间dp 方块消除)

    Blocks Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4250   Accepted: 1704 Descriptio ...

  7. block知识点

    1.block引用局部变量的时候,该变量会作为常量编码到block中,在block中不能被修改. 2.使用 __block修饰的局部变量,不会作为常量被编码到block中,故而在block中可以被修改 ...

  8. free bsd x修改UTC->SCT

    #cp /usr/share/zoneinfo/Asia/Taipei /etc/localtime #ntpdate asia.pool.ntp.org #adjkerntz -a #date

  9. java之CGLIB动态代理

    © 版权声明:本文为博主原创文章,转载请注明出处 CGLIB动态代理: CGLIB动态代理就是对指定的类生成一个子类,覆盖其中所有的方法并环绕增强 优势: - 1. 业务类只需要关注业务逻辑本身,保证 ...

  10. Linux Suse 查看wwn号码的方法

     查看wwn号码 cat /sys/class/fc_host/host*/port_name *代表全部host目录