This repository provides a set of scripts to extract data from a MySQL database, transform it into a CSV file, and integrate it with Google Sheets. The workflow includes database connection, querying, data transformation, and file generation.
- Establishes a connection to a MySQL database.
- Allows querying and data manipulation.
- Key functions:
connect_mysql
: Connects to the database and returns a connection object.
- Manages Google API credentials for accessing Google Sheets.
- Key functions:
credentials
: Loads credentials from a JSON file and returns an authenticated credentials object.
- Stores and retrieves the Google Sheets ID for interaction.
- Key functions:
spreadsheets
: Returns the Google Sheets ID.
- Defines and executes SQL queries on the connected MySQL database.
- Key functions:
amounts_quantity
: Retrieves data about vehicle circulation permits, including vehicle details, payment type, and service module.
- Converts query results into a CSV file.
- Key functions:
sql_to_csv
: Transforms SQL query results into a CSV file namedvehicle_circulation_permit.csv
.
-
Python 3.7 or higher
-
Dependencies Install required libraries:
pip install pymysql pandas google-auth google-auth-oauthlib
-
Google API Credentials
- Obtain a credentials JSON file from the Google Cloud Console.
- Save it as
credentials.json
in the project directory.
-
MySQL Database
- Ensure your database server is running and the credentials are correct in
connect_mysql.py
.
- Ensure your database server is running and the credentials are correct in
- Update the
connect_mysql
function inconnect_mysql.py
with your database details.
- Replace the
SPREADSHEET_ID
inid_spreadsheets.py
with your Google Sheets ID.
- Use
queries.py
to define and execute your SQL queries. - Example:
from queries import amounts_quantity data = amounts_quantity() print(data)
- Run
sql_to_csv.py
to save query results as a CSV file. - Example:
python sql_to_csv.py
- Output:
vehicle_circulation_permit.csv
.
├── connect_mysql.py
├── credentials.py
├── id_spreadsheets.py
├── queries.py
├── sql_to_csv.py
├── credentials.json
├── vehicle_circulation_permit.csv
└── README.md
- Ensure your MySQL server is accessible and credentials are correct.
- For Google Sheets, verify API permissions and access.
- Modify queries in
queries.py
as per your database structure and requirements.
Contributions are welcome! Feel free to open an issue or submit a pull request for improvements.