Data Structure Creation with sqlfaker
sqlfaker is a python library that can be used to generate relational data structures and fill these structures with fake data.
Installation
sqlfaker can be installed via pip from pypi.org. Just run the following command in your terminal or command prompt.
pip install sqlfaker
Class structure
This projet lets you define relational data structures that are build upon the concepts of Database, Table and Column.
A Database can have multiple Table objects which again can each have multible Column objects. There are two classes that inherit from the Column class - ForeignKey and PrimaryKey. These allow you to create key colums.
Usage example
sqlfaker can be used to generate a new database. So far, the Database class only supports MySQL/MariaDB syntax (when it comes to SQL-Export). New database types will be implemented over time.
from sqlfaker.database import Database
# add database
my_db = Database(db_name="campusdb")
# add tables
my_db.add_table(table_name="student", n_rows=500)
my_db.add_table(table_name="studyprogram", n_rows=15)
sqlfaker can be used to also specify the schema of the tables. This is supported by three methods: add_primary_key, add_column and add_foreign_key. Depending on the type of column that should be created, the add methods need different parameters.
The order of column creation must follow consider their interdependencies (much like in SQL). If you e.g. create a foreign key column, you need to create the referenced primary key column first.
# add columns to studyprogram table
my_db.tables["studyprogram"].add_primary_key(column_name="studyprogram_id")
my_db.tables["studyprogram"].add_column(column_name="shortname", data_type="varchar(50)", data_target="name")
my_db.tables["studyprogram"].add_column(column_name="startdate", data_type="date", data_target="date")
# add columns to student table
my_db.tables["student"].add_primary_key(column_name="student_id")
my_db.tables["student"].add_column(column_name="firstname", data_type="varchar(50)", data_target="first_name")
my_db.tables["student"].add_column(column_name="lastname", data_type="varchar(50)", data_target="last_name")
my_db.tables["student"].add_foreign_key(column_name="studiengang_id", target_table="studiengang", target_column="studiengang_id")
As soon as the database structure has been defined, sqlfaker can be used to generate data and then export the complete (or also parts of the) sql script to disk. The syntax of all generated SQL scripts follows MySQL/MariaDB.
data_targetis used to specify the type of fake data that is generated to fill the respective column.sqlfakeruses Pythonfakerfor fake data generation. You can therefore reference allfakerdata types asdata target. Seefaker's documentation for more details.
my_db.generate_data()
my_db.export_sql("test.sql")
Metadata
Release files for sqlfaker 0.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| sqlfaker-0.5.tar.gz | 29.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| sqlfaker-0.5-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 41.2 kB
Release files / sqlfaker-0.5.tar.gz
| Download URL | sqlfaker-0.5.tar.gz |
|---|---|
| Size | 29.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f41d0889c17a4d2a4233e050305901f1ca32b6b84a75c535d642ab8f7182815a
|
|
BLAKE2b-256 checksum How to use checksums |
5ceb4cb7df94300c064f028df4532eaabba92b04496afcf533fab7a7e0a6b7cb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.6
|
Release files / sqlfaker-0.5-py3-none-any.whl
| Download URL | sqlfaker-0.5-py3-none-any.whl |
|---|---|
| Size | 11.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2dca8209d3be0620240e24eff0aca5d8fd9876dd19b8baf4ec8c6a009afc0b71
|
|
BLAKE2b-256 checksum How to use checksums |
b5569035a09eca405c38451d1be7ad4d5c44a333497ba30cd50f5078f10e851c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.6
|