0

I need to create random entries with a given sql-schema in sql with the help of python programming language.

Is there a simple way to do that or do I have to write own generators?

2
  • 3
    There is no built-in function called create_random_entries_in_database, but there is random module, which you can use ;) Commented Dec 15, 2015 at 23:50
  • @Marcel Did my solution work for you? Commented Aug 3, 2016 at 15:11

3 Answers 3

2

You could use Wikipedia as a data source. Select categories that are relevant to your schema and pick random articles from those categories.

This code accesses CatScan using requests for convenience. Maybe there is a library that can do the same (return pages in a Wikipedia category), but writing this short piece of code was easier than finding one.

choice selects a random element from a list.

from random import choice
from requests import post


def title(page):
  return page['a']['title'].split('(')[0].replace('_', ' ').strip()


def category(name, depth=0):
  url = 'https://tools.wmflabs.org/catscan2/catscan2.php'
  payload = {
    'categories': name,
    'depth': depth,
    'format': 'json',
    'doit': 'Do it!',
  }
  category = post(url, data=payload).json()['*'][0]['a']['*']
  return [title(page) for page in category]


first = category('Italian masculine given names')
last = category('Surnames of Italian origin')
work = category('Organized crime members by role')

for i in range(10):
  print(*map(choice, (first, last, work)), sep=',')

The result:

$ python random_data.py | column -t -s,
Santino      Comolli     Boss
Constantino  Furlan      Made man
Ernesto      Forlán      Consigliere
Silvestro    Gherardi    Informant
Adelmo       Mancuso     Bagman
Giuliano     Paganelli   Made man
Renato       Barberis    Capobastone
Roberto      Comollo     Consigliere
Dario        Speroni     Consigliere
Gastone      Pestalozzi  Underboss
Sign up to request clarification or add additional context in comments.

Comments

1

You could try a recently launched Python package called pydbgen. Here is a article about it. Introducing pydbgen: A random dataframe/database table generator

For example, this generate a .DB file which can be used with MySQL or SQLite. The resulting database table was opened in DB Browser for SQLite and looks like following,

myDB.gen_table(db_file='Testdb.DB',table_name='People',
fields=['name','city','street_address','email'])

As you can see, the db file name, the table name, and the fields can be specified.

1 Comment

Also it's important to note that this works only for python 3 installations.
1

You can also use faker. just pip install faker

Just go through documentation and check it out

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.