Giter Site home page Giter Site logo

dbextractor's Introduction

Database Data Extractor

Overview

The Database Data Extractor is a versatile utility designed to connect to various databases, retrieve table or collection data, and export it to CSV or XLSX (Excel) files. It supports PostgreSQL, SQLite, and MongoDB databases (MongoDB support is coming soon).

Features

  • Database Connection: Connects to PostgreSQL, SQLite, and MongoDB databases.
  • Data Retrieval: Retrieves data from specified tables or collections.
  • Data Export: Exports data to CSV or XLSX files.
  • Interactive User Interface: Prompts users for necessary inputs and guides them through the export process.

Requirements

  • Python 3.x
  • Required Python packages: psycopg2, sqlite3, pymongo, openpyxl
  • Access to the respective database servers.

Installation

Install the required Python packages using pip:

pip install psycopg2 pymongo openpyxl

Usage

Connecting to a Database

  1. Database URI:
    • PostgreSQL: postgresql://username:password@host:port/database
    • SQLite: sqlite:///path/to/database.db
    • MongoDB: mongodb+srv://username:password@host:port/database (Support coming soon)

Running the Tool

  1. Start the Script:

    • Run the script from the command line:

      python3 main.py
  2. Enter Database URI:

    • When prompted, enter your database URI. The tool will automatically detect the type of database based on the URI.
  3. Select Table/Collection:

    • The tool will list all available tables (for SQL databases) or collections (for MongoDB). Select the desired table/collection by entering the corresponding number.
  4. Choose Export Format:

    • Choose between CSV (enter c) or XLSX (enter x) as the export format.
  5. Export Data:

    • The tool will export the data from the selected table/collection to the specified format and save it in the current directory.

Example Workflow

  1. Run the tool:

    python main.py
  2. Enter your database URI:

    Enter your database URI: postgresql://username:password@localhost:5432/mydatabase
  3. Select a table:

    Available tables/collections:
    1. table1
    2. table2
    Select a table/collection by number: 1
  4. Choose the export format:

    Do you want to export to CSV (c) or XLSX (x)? c
  5. Export complete:

    Data from table/collection 'table1' has been successfully exported to table1.csv

Functions

parse_database_uri(database_uri)

  • Purpose: Parses the SQLAlchemy database URI and extracts connection parameters.
  • Parameters: database_uri (str) - The database URI.
  • Returns: Dictionary containing host, database, user, and password.

connect_sqlite(db_path)

  • Purpose: Connects to a SQLite database.
  • Parameters: db_path (str) - Path to the SQLite database file.
  • Returns: SQLite connection object.

get_table_names(connection)

  • Purpose: Retrieves the names of all tables/collections from the connected database.
  • Parameters: connection - Database connection object.
  • Returns: List of table/collection names.

export_to_csv(connection, collection_or_table_name, csv_file)

  • Purpose: Exports data from the specified table/collection to a CSV file.
  • Parameters:
    • connection - Database connection object.
    • collection_or_table_name (str) - Name of the table/collection to export.
    • csv_file (str) - Path to the output CSV file.

convert_csv_to_xlsx(csv_file_path, xlsx_file_path)

  • Purpose: Converts a CSV file to an XLSX file.
  • Parameters:
    • csv_file_path (str) - Path to the input CSV file.
    • xlsx_file_path (str) - Path to the output XLSX file.

main()

  • Purpose: Main function that prompts the user for input and coordinates the export process.

Notes

  • MongoDB Support: Currently, MongoDB export functionality is not implemented. Support for MongoDB is coming soon.
  • Temporary Files: When exporting to XLSX, a temporary CSV file is created and then deleted after conversion.

Troubleshooting

  • Database Connection Errors: Ensure that the database URI is correctly formatted and that the database server is accessible.
  • Invalid Input: Follow the prompts carefully and enter valid numbers and choices.

Contributing

If you would like to contribute to this tool or report any issues, please create a pull request or issue on the project's GitHub repository or contact me on Twitter @goodnesskolapo or Telegram @goodnesskolapo

dbextractor's People

Stargazers

superman avatar Musa AbdulKareem avatar

Watchers

superman avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.