The goal is to create a Python project that connects to a MySQL database using a class with specific methods. The project will follow a structured format, including a folder for modules, a folder for credentials (which should be ignored by Git), and a main file for execution. The project will also include a requirements.txt file, a READMEmd file for documentation, and will be pushed to a GitHub repository.
Initial Task
- Create a new virtual environment for these tasks
TASK | ✅ |
---|
-
Create the Project Structure
-
Create the project root folder and the following structure:
database_connections_project/ ├── cred/ ├── modules/ ├── src/ ├── requirements.txt ├── main.py ├── README.md └── .gitignore
-
Task:
-
Create the cred, modules, src folders, requirements.txt, main.py, README.md, and .gitignore files.
-
Ensure the cred/ folder is added to .gitignore to avoid committing sensitive credentials to Git.
TASK | ✅ |
---|
-
Store Credentials in YAML Format
-
In the cred/ folder, create a file named db_credentials.yaml to store the database connection details in the following format:
mysql:
host: "localhost"
user: "root"
password: "your_password"
database: "sakila"
-
Task:
- Create the YAML file and save the MySQL connection credentials.
TASK | ✅ |
---|
-
Implement MySQLConnector Class
- Inside the modules folder, create a file named mysql_connector.py.
- Modify the MySQLConnector class to read the credentials from the YAML file in cred/:
- __ init __ (self, cred_file): Initializes the class, reads the credentials from the YAML file.
- Get_connection(self): Establishes connection with the database and returns a cursor.
- Close_connection(self): Closes the active connection.
- query(self, sql_query): Executes an SQL query and returns a Pandas DataFrame with the results.
- Inside the modules folder, create a file named mysql_connector.py.
Task:
-
Implement the MySQLConnector class inside mysql_connector.py and modify the constructor to read from the YAML file.
-
We read the credentials yaml with:
~with open('yaml route') as file:
-
With yaml function safe_load, we are loading the variables in the file into a variable
~ yamll = yaml.safe_load(file)
-
We can use all the variables in the yaml just by indicate the name of the system an then the variable
~yamll['mysql']['user']
-
TASK | ✅ |
---|
Task:
- Add necessary libraries to requirements.txt.
TASK | ✅ |
---|
-
Write the Main Script (main. py)
-
In the main. py file, create a script to:
-
Initialize the MySQLConnector class, passing the path to the db_credentials.yaml file.
-
Establish a connection to sakila database .
-
Run a sample query on a sample database (e.g., SELECT * FROM actor LIMIT 5;).
-
Print the results as a Pandas DataFrame.
-
Close the connection.
-
Task:
-
Write the code in main. py to test the connection and run queries.
TASK | ✅ |
---|
-
Set Up .gitignore
Task:
-
Ensure the .gitignore file is properly set up to exclude sensitive credential files.
TASK | ✅ |
---|
-
Create a README.md File
Task:
-
Create the README.md file with the necessary details.
TASK | ✅ |
---|
-
Push the Project to GitHub
-
Initialize a Git repository in the project folder:
-
Create a GitHub repository and push the local repository to GitHub:
-
Task:
-
Push the entire project to GitHub and ensure the repository is public for review.
TASK | ✅ |
---|
We agregated a class with conection, selection of DB and two querys, one with mango and another using views created at fauxton for the CouchDB porject and a conditions in the main for a OS selection and a Mysql o CouchDB iteraction.
TASK | ✅ |
---|
We agregate BigQuery to the project, same as CouchDB, we add a class that allow us to create a conection an one method that can let us call querys to the dataset a his tables. Finally we can put directly the return values in a pandas Dataframe
TASK | ✅ |
---|