dbautomate is a Python package that provides flexible, interactive and expressive data manipulation with databases. It designed to make working with "relational" and "non-relational" databases seamless, interactive and efficient, provides a flexible and scalable solution for diverse data storage and retrievel needs.
Here are some of the key features of dbautomate as
follows:
The source code is currently hosted on GitHub at: https://github.com/ravi46931/dbautomatepkg
Install via pip:
# PyPI pip install dbautomate
dbautomate supports Python3.8, Python3.9, python3.11.
Installation requires:
User guide to use MySQL database.
from dbautomate import mysqloperator
mysql_handler = mysqloperator.MySQL_connector()
config = {
'host' : 'your_mysql_host',
'user' : 'your_mysql_user',
'password' : 'your_mysql_password',
'database' : 'your_database_name'
}
conn = mysql_handler( config, attempts = 3, delay = 2)
# attempts: Number of times it tries to connect the server in case of failure.
# delay: time after which next attempt will happen
# These two parameters are optional
For successful connection it gives following message:
print(mysql_handler)
Output:
MySQLHandler Object -
Config: {'host' : 'your_mysql_host', 'user' : 'your_mysql_user', 'password' : 'your_mysql_password', 'database' : 'your_database_name'} Connected: True
query = "SELECT * FROM table_name"
mysql_handler.execute_query(query)
It prints table along with successful execution message.
purchase_price | sale_price |
---|---|
8000 | 9505 |
8500 | 10105 |
7000 | 8505 |
10500 | 11505 |
Insert single entry or multiple entries.
table_name = 'cats'
values = [
('Mena', 10),
('Lisa', 13)
]
mysql_handler.insert_data(table_name, values)
If you want to use different database to insert the data, you can achieve this by mentioning the database.
db_name = 'book'
table_name = 'english_books'
values = [....]
mysql_handler.insert_data(db_name, table_name, values)
You can insert CSV file into the table as well.
filepath='path/to/your/data.csv'
mysql_handler.bulk_insert(table_name, filepath)
# Change the database as well
filepath='path/to/your/data.csv'
db_name='books'
mysql_handler.bulk_insert(table_name, filepath, db_name)
1) If the first entry is autoincrement id, and you have not
provided that in your input data (aka 'values' in above code) then
enter 'y', but if you have provided in your input data then enter
something else.
2) If you are inserting the multiple entries then enter y.
3) If you are inserting the single entry then enter n.
A success message shows after successful insertion of the data.
To save the table locally from the current active database.
table_name = 'cats'
mysql_handler.save_data(table_name)
To save the data from the different database.
db_name = 'books'
table_name = 'english_books'
mysql_handler.save_data(table_name, db_name)
Prints the success message after the saving data.
mysql_handler.close_connection()
It prints successful connection close.
User guide to use MongoDB database.
from dbautomate import mongodboperator
mongo = mongodboperator.MongoDB_connector()
uri = "localhost:27017"
client = mongo.get_mongo_client(uri)
A success message prints.
Ensure you have already created with client.
database_name = "firstdb"
mongo.create_database(database_name)
After creating database it prints success message.
Ensure you have already created with client and database.
collection_name = "first"
mongo.create_collection(collection_name)
After creating collection it prints success message.
You can insert single or multiple entries.
# Single Entry
single_entry = { 'name' : 'abc', 'age' : 5}
mongo.insert_data(single_entry)
A success message prints.
# Multiple Entries
multiple_entries = [{ 'name' : 'abc', 'age' : 5}, { 'name' : 'pqr', 'age' : 8}]
mongo.insert_data(multiple_entries)
A success message prints.
If you want to insert data from the CSV, EXCEL or JSON file, you can achieve this by following way.
# To insert the data in the active collection
mongo.bulk_insert(filepath)
# To insert the data in a new or different collection
collection_name = "employee"
mongo.bulk_insert(filepath, collection_name)
A success message prints.
You can find the data.
mongo.find_data()
1) If you want to see the data in the form of DataFrame enter y.
2) If you want to see the data in the form of List enter n.
You can save the data locally.
mongo.save_data()
Enter the type of the file and name of the file (that you want to save).
Do you want to save the data as json file or csv file?(json/csv) json Enter the filename: data.json
For deleting the entire data from the collection.
mongo.delete_data()
For delete the data based on the key value.
key_value = { 'age' : 5}
mongo.delete_data(key_value)
Enter one if you want to delete the one entry else many it delete all the entry based on the key_value
Do you want delete the one entry or mutiple entries?(one/many) one
If you choose many option then the following message will show
filter_criteria = { 'age' : 13}
update_data = [{ ' $set ' : { 'age' : 17}}]
mongo.update_data_entry(filter_criteria, update_data)
Enter one if you want to update the single entry.
Do you want single entry update on multiple?(one/many) one
Enter many if you want to update the multiple entries.
mongo.close_mongo_client()
After successful closing the client.
For more detail of each of the functions can be reed the docstrings
print(mysql_handler.insert_data.__doc__)
You can check the latest sources with the command:
https://github.com/ravi46931/dbautomatepkg.git