https://github.com/camilajaviera91/sql-to-googlesheets
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.
https://github.com/camilajaviera91/sql-to-googlesheets
csv googlesheets googlesheetsapi lookerstudio mysql python sql
Last synced: 8 months ago
JSON representation
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.
- Host: GitHub
- URL: https://github.com/camilajaviera91/sql-to-googlesheets
- Owner: CamilaJaviera91
- Created: 2024-11-27T01:34:04.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2025-01-03T14:01:49.000Z (10 months ago)
- Last Synced: 2025-01-03T15:19:38.931Z (10 months ago)
- Topics: csv, googlesheets, googlesheetsapi, lookerstudio, mysql, python, sql
- Language: Python
- Homepage:
- Size: 160 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# MySQL and Google Sheets Data Integration
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.
## Files Overview
### 1. `connect_mysql.py`
- 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.### 2. `credentials.py`
- Manages Google API credentials for accessing Google Sheets.
- Key functions:
- `credentials`: Loads credentials from a JSON file and returns an authenticated credentials object.### 3. `id_spreadsheets.py`
- Stores and retrieves the Google Sheets ID for interaction.
- Key functions:
- `spreadsheets`: Returns the Google Sheets ID.### 4. `queries.py`
- 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.### 5. `sql_to_csv.py`
- Converts query results into a CSV file.
- Key functions:
- `sql_to_csv`: Transforms SQL query results into a CSV file named `vehicle_circulation_permit.csv`.## Prerequisites
1. **Python 3.7 or higher**
2. **Dependencies**
Install required libraries:
```bash
pip install pymysql pandas google-auth google-auth-oauthlib
```3. **Google API Credentials**
- Obtain a credentials JSON file from the Google Cloud Console.
- Save it as `credentials.json` in the project directory.4. **MySQL Database**
- Ensure your database server is running and the credentials are correct in `connect_mysql.py`.## Usage
### 1. **Connect to MySQL Database**
- Update the `connect_mysql` function in `connect_mysql.py` with your database details.### 2. **Configure Google Sheets**
- Replace the `SPREADSHEET_ID` in `id_spreadsheets.py` with your Google Sheets ID.### 3. **Execute Queries**
- Use `queries.py` to define and execute your SQL queries.
- Example:
```python
from queries import amounts_quantity
data = amounts_quantity()
print(data)
```### 4. **Generate CSV File**
- Run `sql_to_csv.py` to save query results as a CSV file.
- Example:
```bash
python sql_to_csv.py
```
- Output: `vehicle_circulation_permit.csv`## Project Structure
```plaintext
.
├── connect_mysql.py
├── credentials.py
├── id_spreadsheets.py
├── queries.py
├── sql_to_csv.py
├── credentials.json
├── vehicle_circulation_permit.csv
└── README.md
```## Notes
- 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
Contributions are welcome! Feel free to open an issue or submit a pull request for improvements.