#/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. JAVA常见算法题(十二)

    package com.xiaowu.demo; /** * 完全平方即用一个整数乘以自己例如1*1,2*2,3*3等,依此类推.若一个数能表示成某个整数的平方的形式,则称这个数为完全平方数. * 完 ...

  2. java 中的VO,PO,DTO,DO对象

    经常会接触到VO,DO,DTO的概念,本文从领域建模中的实体划分和项目中的实际应用情况两个角度,对这几个概念进行简析. 得出的主要结论是:在项目应用中,VO对应于页面上需要显示的数据(表单),DO对应 ...

  3. flask的session解读及flask_login登录过程研究

    #!/usr/bin/env python # -*- coding: utf-8 -*- from itsdangerous import URLSafeTimedSerializer from f ...

  4. django 用model来简化form

    django里面的model和form其实有很多地方有相同之处,django本身也支持用model来简化form 一般情况下,我们的form是这样的 from django import forms ...

  5. ODS与EDW的区别

    http://blog.csdn.net/bitcarmanlee/article/details/51013474 根据自己的理解与实际项目经验,说说ODS与EDW的异同.如果有不对的地方,欢迎大家 ...

  6. 转: Servlet-jsp从入门到精通 1~5

    评注: 来自简书,内容很简要. http://www.jianshu.com/p/d718a5f4a850 (一) http://www.jianshu.com/p/06efc32c10cf   (二 ...

  7. agruments应用——求出函数参数的总合&&css函数——设置/读取对象的属性&&当前输入框高亮显

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. Linux服务器大量向外发包问题排查

    最近Linux redhat 6.5 APP 业务系统,向外大量发送流量,不断建立tcp连接,目标地址是美国的一个IP,估计被当成肉鸡了,比较悲惨,直接飞向IDC机房,防火墙显示这个APP服务器tcp ...

  9. Python学习笔记(一)类和继承的使用

    一年前就打算学Python了,折腾来折腾去也一直没有用熟练,主要是类那一块不熟,昨天用Python写了几个网络编程的示例,感觉一下子迈进了很多.这几天把学习Python的笔记整理一下,内容尽量简洁. ...

  10. C# Socket.Connect连接请求超时机制

    介绍 您可能注意到了,.Net的System.Net.Sockets.TcpClient和System.Net.Sockets.Socket都没有直接为Connect/BeginConnect提供超时 ...