Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 81 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
## Lab Three
# Lab Three
Python script to create,read,update,delete (CRUD) a row from a **table**


## Run Locally
# Run Locally

Clone the project

Expand All @@ -19,31 +19,103 @@ Go to the project directory
Install dependencies

```bash
pip install -r requirements.txt
python -m pip install -r requirements.txt
```

Create Table
## Create Table
Hiding database base credentials instead of hard coding by setting the passwords as environment variables
(added this to all of the table code operations)
Install dotenv package
```bash
python -m pip install python-dotenv
```

Create a .env file
```bash
DB_USER=root
DB_PASSWORD=your_secret_password
```

Then add to `create_db.py`
```bash
from dotenv import load_dotenv
import os
import mysql.connector


load_dotenv()

db_user = os.getenv("DB_USER")
db_password = os.getenv("DB_PASSWORD")


# Connect to MySQL Server
conn = mysql.connector.connect(
host="localhost",
user=db_user,
password=db_password
)

# Create a cursor object
cursor = conn.cursor()
```

Make permananent for local development
```bash
export DB_USER="your_mysql_user"
export DB_PASSWORD="your_mysql_password"
```
Edited the code to create the tables in form of a dictionary and count all tables created by the code and sisplay the output
```bash
python create_table.py
```
Insert Table

## Insert Table
Inserts tables innto the created tables

```bash
python insert_table.py
```
Select Rows from a Table

## Select Rows from a Table

```bash
python select.py
```
Update a row from a Table

## Update a row from a Table

```bash
python update_table.py
```
Delete a Row from a Table

## Delete a Row from a Table

```bash
python delete_table.py
```

To be able to update the changes we made to this code, we need to fork the original repository
1. Click *fork* on the original repository
2. Get the fork URL
```bash
git remote set-url origin https://github.com/yourusername-forkedURL/repo.git
```
3. Check which branch we are on
```bash
python delete.py
git remote -v
```
4. Push the new code back to the forked branch/the fork
```bash
git push -u origin main
```
5. Double check to make sure all remote files match the files on your local repository. If not, check then add them using
```bash
git status
git add README.md
git commit -m "Update README content"
git push -u origin main
```
6. Create a pull request on your fork on Github
- Click "Compare and pull request"
- Add a title/description and submit the PR
31 changes: 31 additions & 0 deletions create_db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from dotenv import load_dotenv
import os
import mysql.connector

load_dotenv()

db_user = os.getenv("DB_USER")
db_password = os.getenv("DB_PASSWORD")


# Connect to MySQL Server
conn = mysql.connector.connect(
host="localhost",
user=db_user,
password=db_password
)

# Create a cursor object
cursor = conn.cursor()

# Create a new database
database_name = "movies" # Add a unique Database name
cursor.execute(f"CREATE DATABASE IF NOT EXISTS {database_name}")

print(f"Database '{database_name}' created successfully!")

# Close the connection
cursor.close()
conn.close()


40 changes: 28 additions & 12 deletions create_table.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,52 @@
from dotenv import load_dotenv
import os
import mysql.connector

load_dotenv()

db_user = os.getenv("DB_USER")
db_password = os.getenv("DB_PASSWORD")

# Connect to MySQL Server
conn = mysql.connector.connect(
host="localhost",
database="movies",
user="root",
password=" " # Add your password
user=db_user,
password=db_password
)

# Create a cursor object
cursor = conn.cursor()

# Execute the SQL statement to create the table
cursor.execute("""
CREATE TABLE movie(
# Execute the SQL statement to create the tables
tables = {
"actors": """
CREATE TABLE IF NOT EXISTS movie(
id INT PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(45),
year date
)
""")
cursor.execute("""
CREATE TABLE actors(
);
""",
"movie": """
CREATE TABLE IF NOT EXISTS actors(
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(45),
age INT
)
""")
);
"""
}

# Create tables
created_count = 0

for table_name, create_query in tables.items():
cursor.execute(create_query)
created_count += 1

# Commit the changes
conn.commit()

print("table created successfully")
print(f"{created_count} tables created successfully")

# Close the connection
cursor.close()
Expand Down
11 changes: 9 additions & 2 deletions delete.py → delete_table.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
from dotenv import load_dotenv
import os
import mysql.connector
from mysql.connector import Error

load_dotenv()

db_user = os.getenv("DB_USER")
db_password = os.getenv("DB_PASSWORD")

def delete_book(book_id):
"""Delete a movie from the movies table by ID."""

Expand All @@ -12,8 +19,8 @@ def delete_book(book_id):
with mysql.connector.connect(
host="localhost",
database="movies",
user="root",
password=" " # Add your password
user=db_user,
password=db_password
) as conn:
with conn.cursor() as cursor:
cursor.execute(query, data)
Expand Down
11 changes: 9 additions & 2 deletions insert_table.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
from dotenv import load_dotenv
import os
import mysql.connector

load_dotenv()

db_user = os.getenv("DB_USER")
db_password = os.getenv("DB_PASSWORD")

# Connect to MySQL Server
conn = mysql.connector.connect(
host="localhost",
database="movies",
user="root",
password=" " # Add your password
user=db_user,
password=db_password
)

def insert_books(book_list):
Expand Down
11 changes: 9 additions & 2 deletions select.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
from dotenv import load_dotenv
import os
import mysql.connector

load_dotenv()

db_user = os.getenv("DB_USER")
db_password = os.getenv("DB_PASSWORD")

def query_table():
"""Fetches and displays all records from the movies table."""
try:
# Connect to MySQL Server
conn = mysql.connector.connect(
host="localhost",
database="movies",
user="root",
password=" " # Add Password
user=db_user,
password=db_password
)
cursor = conn.cursor()

Expand Down
11 changes: 9 additions & 2 deletions update_table.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
from dotenv import load_dotenv
import os
import mysql.connector
from mysql.connector import Error

load_dotenv()

db_user = os.getenv("DB_USER")
db_password = os.getenv("DB_PASSWORD")

def update_movie(movie_id, new_name):
"""Update the movie title in the movie table."""

Expand All @@ -15,8 +22,8 @@ def update_movie(movie_id, new_name):
with mysql.connector.connect(
host="localhost",
database="movies",
user="root",
password=" " #Add password
user=db_user,
password=db_password
) as conn:
with conn.cursor() as cursor:
cursor.execute(query, data)
Expand Down