Package: dbautomate
Aboutdbautomateis 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.Table of ContentsMain FeaturesWhere to get itInstallationDependenciesHow to use itImport the library for MySQLCreate an instance of MySQL classConnect to MySQL ServerCheck the MySQL handler objectExecute query with MySQLTo insert the data in tableTo save the data of the tableTo close the MySQL connectionImport the library for MongoDBCreate an instance of MongoDB classGet the client of MongoDBCreate database of MongoDBCreate collection of MongoDBTo insert the data in collectionTo find the data of the collectionTo save the data of the collectionTo delete the data of the collectionTo update the data of the collectionTo close the mongo clientFunctionalityDevelopmentImportant linksSource codeMain FeaturesHere are some of the key features ofdbautomateas follows:Thisdbautomateworks withMongoDBas well asMySQLdatabases.When working withMySQL, it can executes any query, it can insert multiple type of data, it can save the data as well.When working withMongoDB, it createsdatabaseandcollection, data can be insert, find, delete, and update as well, the data can be saved locally.Where to get itThe source code is currently hosted onGitHubat:https://github.com/ravi46931/dbautomatepkgInstallationInstall via pip:# PyPI
pip install dbautomateDependenciesdbautomatesupports Python3.8, Python3.9, python3.11.Installation requires:pymongopymongo[srv]dnspythonpandasnumpytermcolormysql-connector-pythonprettytableHow to use itLet suppose you want to useMySQLdatabase.Import the library for MySQLfromdbautomateimportmysqloperatorCreate an instance of MySQL classmysql_handler=mysqloperator.MySQL_connector()Connect to MySQL Serverconfig={'host':'your_mysql_host','user':'your_mysql_user','password':'your_mysql_password','database':'your_database_name'}conn=mysql_handler.connect_to_mysql(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 optionalFor successful connection it gives following message:Connected successfully....Check the MySQL handler objectprint(mysql_handler)Output:MySQLHandler Object-Config:{'host': 'your_mysql_host', 'user': 'your_mysql_user', 'password': 'your_mysql_password', 'database': 'your_database_name'}Connected:TrueExecute query with MySQLquery="select * from table_name"mysql_handler.execute_query(query)It prints table along with successful execution message.Query executed successfully....+----------------+------------+
| purchase_price | sale_price |
+----------------+------------+
| 8000           | 9505       |
+----------------+------------+
| 8500           | 10105      |
+----------------+------------+
| 7000           | 8505       |
+----------------+------------+
| 10500          | 11505      |
+----------------+------------+To insert the data in tableInsert single entry or multiple entries.table_name='cats'values=[('Mena',5),('Kena',11)]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(table_name,values,db_name)You can insertCSVfile into the table as well.filepath='path/to/your/data.csv'mysql_handler.bulk_insert(table_name,filepath)# Change the database as wellfilepath='path/to/your/data.csv'db_name='books'mysql_handler.bulk_insert(table_name,filepath,db_name)If the first entry isautoincrementid, 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.If you are inserting the multiple entries then entery.If you are inserting the single entry then entern.A success message shows after successful insertion of the data.Inserted successfully....To save the data of the tableTo save thetablelocally from the current activedatabase.table_name='cats'mysql_handler.save_data(table_name)To save the data from the differentdatabase.db_name='animals'table_name='cats'mysql_handler.save_data(table_name,db_name)Prints the success message after the saving data.Enter the filename: data.csvFile:'data.csv'saved successfully....To close the MySQL connectionmysql_handler.close_connection()It prints successful connection close.MySQL connection closed.Let suppose you want to useMongoDBdatabase.Import the library for MongoDBfromdbautomateimportmongodboperatorCreate an instance of MongoDB classmongo=mongodboperator.MongoDB_connector()Get the client of MongoDBuri="localhost:27017"client=mongo.get_mongo_client(uri)A success message prints.Connected to MongoDB Successfully....Create database of MongoDBEnsure you have already created with client.database_name='firstdb'mongo.create_database(database_name)After creating database it prints success message.Database created successfully....Create collection of mongoDBEnsure you have already created with client and database.collection_name='first'mongo.create_collection(collection_name)After creating collection it prints success message.Collection created successfully....To insert the data in collectionYou can insert single or multiple entries.# Single Entrysingle_entry={'name':'abc','age':24}mongo.insert_data(single_entry)A success message prints.Inserted successfully(Single entry)....# Multiple Entriesmultiple_entries=[{'name':'abc','age':24},{'name':'def','age':22}]mongo.insert_data(multiple_entries)A success message prints.Data inserted successfully....If you want to insert data from theCSV,EXCELorJSONfile, you can achieve this by following way.# To insert the data in the current collectionmongo.bulk_insert(filepath)# To insert the data in a new or different collectioncollection_name='employee'mongo.bulk_insert(filepath,collection_name)A success message prints.Data inserted successfully....To find the data of the collectionYou can find the data.mongo.find_data()If you want to see the data in the form of DataFrame entery.If you want to see the data in the form of List entern.To save the data of the collectionYou 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)jsonEnter the filename: q.jsonFile:'q.json'saved successfully....To delete the data of the collectionFor deleting the entire data from the collection.mongo.delete_data()Enteryif you want to delete the entire data.It will delete entire data, Do you want to delete the data?(y/n):yAll entry deletedFor delete the data based on the key value.key_value={'age':25}mongo.delete_data(key_value)Enteroneif you want to delete the one entry elsemanyit delete all the entry based on thekey_valueDo you want delete the one entry or mutiple entries?(one/many) oneEntry deleted successfullyIf you choosemanyoption then the following message will showMultiple entries deleted successfullyTo update the data of the collectionfilter_criteria={'age':25}update_data=[{'$set':{'age':24}}]mongo.update_data_entry(filter_criteria,update_data)Enteroneif you want to update the single entry.Do you want single entry update on multiple?(one/many) oneUpdated Successfully (one entry)...Entermanyif you want to update the multiple entries.Do you want single entry update on multiple?(one/many) manyUpdated Successfully (multiple entries)....To close the mongo clientmongo.close_mongo_client()After successful closing the client.MongoDB client closed successfully..FunctionalityFor more detail of each of the functions can be reed the docstringsprint(mysql_handler.insert_data.__doc__)DevelopmentImportant linksSource code repo:GitHub LinkDownload Release:PypiBugs/ Feature requests:GitHub issue TrackerSource codeYou can check the latest sources with the command:https://github.com/ravi46931/dbautomatepkg.gitGo to top