Skip to content

Instantly share code, notes, and snippets.

@madawei2699
Created April 1, 2020 13:07
Show Gist options
  • Save madawei2699/aa5d0f8966c033e72d86462184f284c9 to your computer and use it in GitHub Desktop.
Save madawei2699/aa5d0f8966c033e72d86462184f284c9 to your computer and use it in GitHub Desktop.
populate millions records into mysql database
from peewee import *
import uuid
import random
import string
from datetime import date, timedelta
db_name = 'db'
table_name = 'table'
db = MySQLDatabase(db_name, user='root', password='123456', host='localhost', port=3307)
class BaseModel(Model):
"""A base model that will use our MySQL database"""
class Meta:
database = db
class VehicleSimUsage(BaseModel):
date = DateField()
vin = CharField()
apn1 = IntegerField()
apn2 = IntegerField()
voiceCall = IntegerField()
duration = IntegerField()
class Meta:
table_name = table_name
primary_key = CompositeKey('date', 'vin')
db.connect()
def main():
for j in range(20000):
data_source = []
vin = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(17))
print("{}: {}".format(j, vin))
for i in range(500):
dict = {'date': date.today() - timedelta(days=i), 'vin': vin, 'apn1': random.randint(1, 1000), 'apn2': random.randint(1, 1000), 'voiceCall': random.randint(1, 100), 'duration': random.randint(1, 100)}
data_source.append(dict)
VehicleSimUsage.insert_many(data_source).execute()
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment