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?
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
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.
create_random_entries_in_database, but there israndommodule, which you can use ;)