python连接mysql循环插入千万条数据脚本
之前都是在mysql的存储过程中插入数据,毕竟mysql语法函数有限,很多都有限制。突然想到学了python正好可以练练手。
首先需要安装pymysql模块包(模块包安装请自行百度)
#encoding:utf-8 import pymysql import time import random #连接mysql db = pymysql.connect(‘14.29.249.74‘,‘kx_admin‘,‘Yjh7yC0L7L1G1k6qXKwb‘,‘ilock‘) #获取mysql操作光标 cursor = db.cursor() #初始化变量 count = 0 #设置sql语句循环次数 while count <= 100: count += 1 #定义mysql字段的范围随机数变量 num = random.randint(0,2) memo_num = random.randint(100,111) city_list = [‘长沙‘,‘湘潭‘,‘株洲‘,‘衡阳‘] a = random.choice(city_list) #生成mysql语句插入语句 sql = "insert into iodn_city(city_code,city_name,memo)values(%s,‘%s‘,%s)" %(num,a,memo_num) #执行sql语句 try: cursor.execute(sql) db.commit() #错误回滚 except: db.rollback() #关闭mysql db.close()
相关推荐
CoderToy 2020-11-16
emmm00 2020-11-17
王艺强 2020-11-17
ribavnu 2020-11-16
bianruifeng 2020-11-16
wangshuangbao 2020-11-13
苏康申 2020-11-13
vivenwan 2020-11-13
moyekongling 2020-11-13
云中舞步 2020-11-12
要啥自行车一把梭 2020-11-12
aydh 2020-11-12
kuwoyinlehe 2020-11-12
minerk 2020-11-12
vitasfly 2020-11-12
jazywoo在路上 2020-11-11
敏敏张 2020-11-11
世樹 2020-11-11