生成随机内容用到的方法:

substr是一个字符串函数,从第二个参数1,开始取字符,取到3 + floor(rand() * 75)结束

floor函数代表的是去尾法取整数。

rand()函数代表的是从0到1取一个随机的小数。

    -- rand() * 75就代表的是:0到75任何一个小数,
-- 3+floor(rand() * 75)就代表的是:3到77的任意一个数字
concat()函数是一个对多个字符串拼接函数。 sha1是一个加密函数,sha1(rand())对生成的0到1的一个随机小数进行加密,转换成字符串的形式。
-- concat(sha1(rand()), sha1(rand()))就代表的是:两个0-1生成的小数加密然后进行拼接。 substr(concat(sha1(rand()), sha1(rand())), 1, floor(rand() * 80))就代表的是:从一个随机生成的一个字符串的第一位开始取,取到(随机3-77)位结束。 case floor(rand()*10) mod 2 when 1 then 'M' else 'F' end,代表:当余数为1是,就取M,其他的为F

sql语句说明

TMP='''
set @i :=0;
create table TMP as select (@i := @i + 1) as id from information_schema.tables limit 10;
'''
nformation_schema.tables表:提供了关于数据库中的表的信息(包括视图)。详细表述了某个表属于哪个schema,表类型,表引擎,创建时间等信息。是show tables from schemaname的结果取之此表

mysql中变量不用事前申明,在用的时候直接用“@变量名”使用就可以了。set这个是mysql中设置变量的特殊用法,当@i需要在select中使用的时候,必须加:,这样就创建好了一个表tmp

示例:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time : 2017/11/22 23:51
# @Author : lijunjiang
# @File : insert.py import MySQLdb # SQL
# 向Student表中从100开始插入1000条随机数据
Student='''
set @i := 100;
insert into Student select @i := @i + 1, substr(concat(sha1(rand()*10) + sha1(rand())),1,3+floor(rand()*75)),case floor(rand()*10) mod 2 when 1 then 'M' else 'F' end, 20+floor(rand() * 5) from TMP a,TMP b,TMP c;
''' # 向 Cours 表中从10开始插入10条随机数据
Cours='''
set @i := 10;
insert into Course select @i := @i + 1, substr(sha1(rand()),1,2+floor(rand()*10)), 1 + floor(rand()*10) from TMP a;
''' #向 Score 表中从200开始插入10000条数据
Score='''
set @i := 200;
insert into Score select @i := @i + 1, floor(101+rand()*1000), floor(11+rand()*10),floor(1+ rand()*100) from TMP a, TMP b, TMP c, TMP d;
''' # 向Teacher 表中从1 开始插入100条数据
Teacher='''
set @i := 0;
insert into Teacher select @i := @i + 1, substr(sha1(rand()),1,3+floor(rand() * 10));
''' def connect_mysql():
db_config = dict(host="11.11.11.11", port=3306, db="python", charset="utf8", user="python", passwd="python")
try:
cnx = MySQLdb.connect(**db_config)
except Exception as err:
raise err
return cnx if __name__ == "__main__":
# sql = "create table test(id int not null);"
cnx = connect_mysql() # 连接mysql
# cus = cnx.cursor() # 创建一个游标对象
try:
cus = cnx.cursor()
cus.execute(Student)
cus.close() cus = cnx.cursor()
cus.execute(Teacher)
cus.close() cus = cnx.cursor()
cus.execute(Score)
cus.close() cus = cnx.cursor()
cus.execute(Cours)
cus.close() cnx.commit()
except Exception as err:
cnx.rollback()
raise err
finally:
cnx.close()
mysql> select * from Student;
+-------+-----------------------+--------+------+
| StdID | StdName | Gender | Age |
+-------+-----------------------+--------+------+
| 101 | 1.79769313486232e+308 | M | 22 |
| 102 | 97 | F | 21 |
| 103 | 4.12e+72 | M | 24 |
| 104 | 18791937 | F | 24 |
| 105 | 88 | F | 21 |
| 106 | 159612 | M | 24 |
| 107 | 93 | F | 21 |
| 108 | 527 | F | 20 |
| 109 | 4 | M | 21 |
| 110 | 1.79769313486232e+308 | F | 23 | | 1099 | 11515 | F | 24 |
| 1100 | 1.79769313486232e+308 | F | 20 |
+-------+-----------------------+--------+------+
1000 rows in set (0.00 sec) mysql> select * from Teacher;
+-----+--------------+
| TID | TName |
+-----+--------------+
| 1 | 8dcd0f6f4c67 |
| 2 | 852c304e |
| 3 | 23cdcaf356e | | 99 | 21bcef63 |
| 100 | 77b76fa88f |
+-----+--------------+
100 rows in set (0.00 sec) mysql> select * from Score limit 5; +-----+-------+-------+-------+
| SID | StdID | CouID | Grade |
+-----+-------+-------+-------+
| 201 | 726 | 18 | 4 |
| 202 | 951 | 12 | 11 |
| 203 | 238 | 14 | 43 | mysql> select * from Score;
| 10199 | 1010 | 15 | 67 |
| 10200 | 970 | 14 | 23 |
+-------+-------+-------+-------+
10000 rows in set (0.01 sec) mysql> select * from Course;
+-------+------------+-----+
| CouID | CName | TID |
+-------+------------+-----+
| 11 | bd3ffa | 2 |
| 12 | eb8c | 7 |
| 13 | aab5381 | 1 |
| 14 | 490ed47b02 | 3 |
| 15 | 16328013b | 1 |
| 16 | a77a91f | 7 |
| 17 | 226b67b68 | 6 |
| 18 | e36f | 4 |
| 19 | 7987545374 | 5 |
| 20 | 424b4b1a8 | 7 |
+-------+------------+-----+
10 rows in set (0.00 sec)

python 向mysql插入数据的更多相关文章

  1. Python中MySQL插入数据

    sql = 'INSERT INTO course(class_name, credit, properties, teacher_name, college_given, classroom) ' ...

  2. 解决Python往MySQL插入中文时报错的问题

    今天遇到一个问题,用Python往MySQL插入数据时,若数据中包含中文会报类似下面的错误: ERROR 1366: Incorrect string value: '\xE4\xB8\xAD\xE5 ...

  3. 十一、MySQL 插入数据

    MySQL 插入数据 MySQL 表中使用 INSERT INTO SQL语句来插入数据. 你可以通过 mysql> 命令提示窗口中向数据表中插入数据,或者通过PHP脚本来插入数据. 语法 以下 ...

  4. mysql 插入数据失败防止自增长主键增长的方法

    mysql设置了自增长主键ID,插入失败的那个自增长ID也加一的,比如失败5个,下一个成功的不是在原来最后成功数据加1,而是直接变成加6了,失败次数一次就自动增长1了,能不能让失败的不增长的? 或者说 ...

  5. mysql插入数据与删除重复记录的几个例子(收藏)

    mysql插入数据与删除重复记录的几个例子 12-26shell脚本实现mysql数据的批量插入 12-26mysql循环语句插入数据的例子 12-26mysql批量插入数据(insert into ...

  6. shell脚本获取mysql插入数据自增长id的值

    shell脚本获取mysql插入数据自增长id的值 在shell脚本中我们可以通过last_insert_id()获取id值,但是,需要注意的是,该函数必须在执行插入操作的sql语句之后,立即调用,否 ...

  7. MySQL 插入数据

    MySQL 插入数据 MySQL 表中使用 INSERT INTO SQL语句来插入数据. 你可以通过 mysql> 命令提示窗口中向数据表中插入数据,或者通过PHP脚本来插入数据. 语法 以下 ...

  8. mysql插入数据时,中文乱码

    MySQL 插入数据时,中文乱码问题的解决(转) 当向 MySQL 数据库插入一条带有中文的数据形如 insert into employee values(null,'张三','female','1 ...

  9. mysql插入数据后返回自增ID的方法,last_insert_id(),selectkey

    mysql插入数据后返回自增ID的方法 mysql和oracle插入的时候有一个很大的区别是,oracle支持序列做id,mysql本身有一个列可以做自增长字段,mysql在插入一条数据后,如何能获得 ...

随机推荐

  1. label标签的作用

    在用户注册的时候,常常用户点击文字就需要将光标聚焦到对应的表单上面,这个是怎么实现的呢?就是下面我要介绍的<label>标签的for属性 定义:for 属性规定 label 与哪个表单元素 ...

  2. React-router4简约教程

    React-router4简约教程 教程       webEmmet 17年10月   React-router和React-router-dom的选择 很多刚使用react的同学在接触到react ...

  3. 最“高大上”的Spring测试:Spring Test

    我想给大家介绍一款非常实用.且高端大气上档次的spring测试,在这里,我要强烈推荐使用Spring的Test Context框架,为什么呢?俗话说,“货比三家不上当”,要搞清楚这个问题,我们先来看一 ...

  4. CodeIgniter学习笔记三:扩展CI的控制器、模型

    一.扩展CI中的控制器 有时需要对CI中的控制器作统一操作,如进行登录和权限验证,这时就可以通过扩展CI控制器来实现. 扩展CI控制器只需要在application/core文件夹中建一个继承自CI_ ...

  5. shell脚本批量下载资源并保留路径

    示例资源列表 如url.txt: http://su.bdimg.com/static/superplus/img/logo_white_ee663702.png http://su.bdimg.co ...

  6. webstrom11 vue插件配置

    直接上图 1. 安装vue插件 2.添加模板 3.指定模板类型 最新的是插件 是 vue.js 创建完 Vue File 文件后 需要在 下面这里关联一下

  7. python学习笔记-基础

    1.大小写敏感 2. print (n,f,s1,s2,s3,s4,sep='\n')  -- 换行输出  seq='\n' print ('n=%d'%n,'f=%f'%f,'s1=%s'%s1,' ...

  8. Python学习-day18 Web框架

    众所周知,对于所有的Web应用,本质上其实就是一个socket服务端,用户的浏览器其实就是一个socket客户端. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 1 ...

  9. java课后作业2017.10.20

    动手动脑1: public class Test{ public static void main(String args[]) { Foo obj1=new Foo(); }}class Foo{ ...

  10. 软工实践 - 第十九次作业 Alpha 冲刺 (10/10)

    队名:起床一起肝活队 组长博客:https://www.cnblogs.com/dawnduck/p/10046955.htmlz 作业博客:班级博客本次作业的链接 组员情况 组员1(队长):白晨曦 ...