database_pandas
Package for working with databases in Python, based on pandas and sqlalchemy.
Details
This package is created for:
- Connection simplification between database and python(mostly jupyter notebook)
- More convinient way for select and insert statements
Package is based on sqlaclhemy and pandas.
Functionality:
Right now consists of 1 class only(for MySQL database):
You can import class the following way:
from database_pandas import MySQLDatabase
MySQLDatabase class consists of the following arguments(user-defined):
- database - schema name.
- drivername - in this cas mysql.
- username - username of the database.
- password - password to the database.
- host - host of the database.
MySQLDatabase class consists of following functions:
- get_url - retrieves back connection string to your database.
- connect_database - connects to your database.
- disconnect_database - disconnects to your database.
- select_sql - runs sql statement.
- insert_sql - isnert pandas dataframe into user-defined table.
- get_tables_regex - gets tables in the schema based on the (optional)regex expression.
for more detailed information look use name_of_function.doc
Example:
Initialisation and data base connection:
from database_pandas import MySQLDatabase
your_database = MySQLDatabase(database = database_name,username = db_username,password = db_password,host = db_host)
your_database.connect_database()
Performing select * from:
your_database.select_sql('select * from table')
Performing insert into table:
Function consists of the following arguments:
-
data_frame - which dataframe to insert.
-
sql_table - in which table to insert.
-
if_exists(default = 'append'):
possible values: ‘fail’, ‘replace’, ‘append’} How to behave if the table already exists.
- fail: Raise a ValueError.
- replace: Drop the table before inserting new values.
- append: Insert new values to the existing table.
- index - Write DataFrame index as a column.
your_database.insert_sql(data_frame,'table_name',if_exists = 'replace')
Get list of tables in schema
**kwargs argument allow you to put as many where operation in select statement as possible. But you have to define where operators(like 'and'/'or')with numbers. For example 'and' operator will be 'and1' or operator will be 'or1'.
Example:
tables = your_database.get_tables_regex(and1 = 'some_regex',and2 = 'some_regex')