MySQL数据库批量写入python脚本

MySQL数据库批量写入python脚本

这是一个个人信息的数据库批量写入的脚本,随机生成的一些数据,如有变动,可以在代码上稍作修改即可

import pymysql
import random
db = pymysql.connect(
    user = "root",
    password = "qwer",
    host = "localhost",
    database = "utf8"
)
cursor = db.cursor()
IDnumber_pre = "1010"
Phone_pre = "(86)1"
Country_pre = "CN"
Address_pre = "RM 1-10,THRD,HY District,WH"
randnum = ""
for i in range(100):
    randnum += str(random.randint(0,9))
for i in range(1):
    IDnumber = IDnumber_pre + ''.join(random.sample(randnum,14))
    name = (''.join(random.sample("abcdefghijklmnopqrstuvwxyz",8))).title()
    Phone = Phone_pre + ''.join(random.sample(randnum,10))
    Country = random.choice(["CN", "US", "UK"])
    Address = Address_pre
    Email = ''.join(random.sample("abcdefghijklmnopqrstuvwxyz123456789" , random.randint(5,15))) + "@" + random.choice(["qq.com", "163.com", "wuhan.com"])
    DataOfBirth = str(random.randint(1920,2020)) + "-" +  str(random.randint(1,12)) + "-" + str(random.randint(1,28))
    print(IDnumber, name, Phone, Country, Address, Email, DataOfBirth)
    sql = "insert into PassengerInfo(IDNumber,Name,PhoneNumber,Country,Address,Email,DataOfbirth) value('%s','%s','%s','%s','%s','%s','%s')" % (IDnumber, name, Phone, Country, Address, Email, DataOfBirth)
    try:
        cursor.execute(sql)
    except Exception as e:
        print(e)
    else:
        print(sql)
db.commit()
cursor.close()
db.close()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值