Skip to content

Instantly share code, notes, and snippets.

@gennad
Created June 8, 2012 07:32
Show Gist options
  • Save gennad/2894234 to your computer and use it in GitHub Desktop.
Save gennad/2894234 to your computer and use it in GitHub Desktop.
Python database populate
import random
import MySQLdb
db=MySQLdb.connect(db="task", user='root')
def get_query():
def get_date():
year = str(random.randint(2011, 2012))
month = str(random.randint(1, 12))
if len(month) == 1: month = '0' + month
day = str(random.randint(1, 28))
if len(day) == 1: day = '0' + str(day)
return year + '-' + month + '-' + day
def get_expires():
year = '2012'
month = random.choice(['05', '06'])
day = str(random.randint(1, 28))
if len(day) == 1: day = '0' + str(day)
return year + '-' + month + '-' + day + ' 12:30'
route_id = random.randint(1, 100)
city_from = random.choice(['ABC', 'CDE', 'QWE', 'RTY', 'GHJ', 'TYU'])
city_to = random.choice(['ABC', 'CDE', 'QWE', 'RTY', 'GHJ', 'TYU'])
date_direct = get_date()
date_back = get_date()
seller = random.randint(1, 100)
route_id = random.randint(1, 1000)
expires = get_expires()
cost = random.randint(1, 1000)
q = "INSERT INTO tickets VALUES ({}, '{}', '{}', '{}', '{}', {}, '{}', {})".format(
route_id, city_from, city_to, date_direct, date_back, seller, expires, cost)
return q
c = db.cursor()
for i in xrange(1000):
c.execute(get_query())
db.commit()
db.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment