python 向mysql插入数据
生成随机内容用到的方法:
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插入数据的更多相关文章
- Python中MySQL插入数据
sql = 'INSERT INTO course(class_name, credit, properties, teacher_name, college_given, classroom) ' ...
- 解决Python往MySQL插入中文时报错的问题
今天遇到一个问题,用Python往MySQL插入数据时,若数据中包含中文会报类似下面的错误: ERROR 1366: Incorrect string value: '\xE4\xB8\xAD\xE5 ...
- 十一、MySQL 插入数据
MySQL 插入数据 MySQL 表中使用 INSERT INTO SQL语句来插入数据. 你可以通过 mysql> 命令提示窗口中向数据表中插入数据,或者通过PHP脚本来插入数据. 语法 以下 ...
- mysql 插入数据失败防止自增长主键增长的方法
mysql设置了自增长主键ID,插入失败的那个自增长ID也加一的,比如失败5个,下一个成功的不是在原来最后成功数据加1,而是直接变成加6了,失败次数一次就自动增长1了,能不能让失败的不增长的? 或者说 ...
- mysql插入数据与删除重复记录的几个例子(收藏)
mysql插入数据与删除重复记录的几个例子 12-26shell脚本实现mysql数据的批量插入 12-26mysql循环语句插入数据的例子 12-26mysql批量插入数据(insert into ...
- shell脚本获取mysql插入数据自增长id的值
shell脚本获取mysql插入数据自增长id的值 在shell脚本中我们可以通过last_insert_id()获取id值,但是,需要注意的是,该函数必须在执行插入操作的sql语句之后,立即调用,否 ...
- MySQL 插入数据
MySQL 插入数据 MySQL 表中使用 INSERT INTO SQL语句来插入数据. 你可以通过 mysql> 命令提示窗口中向数据表中插入数据,或者通过PHP脚本来插入数据. 语法 以下 ...
- mysql插入数据时,中文乱码
MySQL 插入数据时,中文乱码问题的解决(转) 当向 MySQL 数据库插入一条带有中文的数据形如 insert into employee values(null,'张三','female','1 ...
- mysql插入数据后返回自增ID的方法,last_insert_id(),selectkey
mysql插入数据后返回自增ID的方法 mysql和oracle插入的时候有一个很大的区别是,oracle支持序列做id,mysql本身有一个列可以做自增长字段,mysql在插入一条数据后,如何能获得 ...
随机推荐
- Android Config通用类来记录信息
1.整体分析 1.1.源代码,可以直接Copy. public class Config { private static int M = 1024 * 1024; private volatile ...
- poj3348 Cows 凸包 叉积求多边形面积
graham扫描法,参考yyb #include <algorithm> #include <iostream> #include <cstdio> #includ ...
- FileStream流媒体
class Program { static void Main(string[] args) { string source = @"mana.mp4"; string targ ...
- viewDidLoad dispatch_sync
- (void)viewDidLoad { [super viewDidLoad]; NSLog(@"1"); dispatch_sync(dispatch_get_main_qu ...
- 剑指Offer - 九度1514 - 数值的整数次方
剑指Offer - 九度1514 - 数值的整数次方2013-11-30 00:49 题目描述: 给定一个double类型的浮点数base和int类型的整数exponent.求base的exponen ...
- 剑指Offer - 九度1361 - 翻转单词顺序
剑指Offer - 九度1361 - 翻转单词顺序2013-11-23 02:45 题目描述: JOBDU最近来了一个新员工Fish,每天早晨总是会拿着一本英文杂志,写些句子在本子上.同事Cat对Fi ...
- python之if测试
(一)python的条件判断语句一般格式如下: if (条件1): (执行结果) elif(条件2): (执行结果) ..... else: (执行结果) 执行顺序为从上到下判断,若条件1不符合则进入 ...
- 【志银】#define lowbit(x) ((x)&(-x))原理详解
分析下列语句 #define lowbit(x) ((x)&(-x)) 可写成下列形式: int Lowbit(x) { return x&(-x); } 例1:x = 1 十进制转二 ...
- 聊聊、Mybatis XML
引入相应的依赖包 <dependency><groupId>org.mybatis</groupId><artifactId>mybatis-sprin ...
- 【转】Itween 贝塞尔曲线(一)
原地址:点击打开链接 1.回调函数 回调函数,即当动画完成时那瞬间需要执行一次的一个函数,it中默认有一个onComplete函数,当动画完成时会自动执行,且你可以提供需要传递的一些参数.见如下代码: ...