python连接mysql数据库
# -*- coding:utf-8 -*-
'''
pymysql操作mysql实践,参考 http://www.cnblogs.com/wupeiqi/articles/5713330.html
'''
import pymysql
import random
# 创建连接
conn = pymysql.connect(host='172.16.64.3', port=3306, user='jt_test', passwd='cBTFW@4a2Lpn', db='jt_user_center', charset='utf8')
# 创建游标
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) # 使用字典类型获取游标数据
# 执行SQL,并返回受影响行数
count=0
for i in range(1, 100000000):
user_id = 600000000000000000 + i
company_id = random.randint(400000000000000001,400000000001000001) # 隶属100w个公司
tel = 15000000000+random.randint(10000000,50000000)
openid = 500000000000000000+i # 性别
cursor.execute('''
insert into user_info(`user_id`,`company_id`,`nick_name`,`head_img_url`,`tel`,`sex`,`wx_id`,`openid`,`country`,`province`,`city`)
values(%s, %s, 'tommy','https://img.huxiucdn.com/article/1234.jpg', %s,'0','test155', %s,'中国','广东','深圳');
''', (user_id, company_id, tel, openid))
count += 1
if (count == 10000):
print("已写入%s数据..." % i)
conn.commit()
count += 1
if(count==10000):
print("已写入%s数据..."% i)
conn.commit()
count=0
# 提交,不然无法保存新建或者修改的数据s
conn.commit()
# 关闭游标
cursor.close()
# 关闭连接
conn.close()