In this project, I learned about how object-relational mapping is used for database scripting. I became familiar with using MySQLdb and SQLAlchemy to query, create, edit, and delete tables in MySQL.
-
0. Get all states
- 0-select_states.py: Python script that uses MySQLdb
to list all states in the database
hbtn_0e_0_usa. - Usage:
./0-select_states.py <mysql username> <mysql password> <database name>. - Results are ordered by ascending
states.id.
- 0-select_states.py: Python script that uses MySQLdb
to list all states in the database
-
1. Filter states
- 1-filter_states.py: Python script that uses MySQLdb
to list all states with names starting with
Nin the databasehbtn_0e_0_usa. - Usage:
./1-filter_states.py <mysql username> <mysql password> <database name>. - Results are ordered by ascending
states.id.
- 1-filter_states.py: Python script that uses MySQLdb
to list all states with names starting with
-
2. Filter states by user input
- 2-my_filter_states.py: Python script that uses
MySQLdb to display all values matching a given name in the
statestable of the databasehbtn_0e_0_usa. - Usage:
./2-my_filter_states.py <mysql username> <mysql password> <database name> <state name searched>. - Results are ordered by ascending
states.id. - Uses string formatting to construct the SQL query.
- 2-my_filter_states.py: Python script that uses
MySQLdb to display all values matching a given name in the
-
3. SQL Injection...
- 3-my_safe_filter_states.py: Python script
that uses MySQLdb to display all values matching a given name in the
statestable of the databasehbtn_0e_0_usa. - Usage:
./3-my_safe_filter_states.py <mysql username> <mysql password> <database name> <state name searched>. - Results are ordered by ascending
states.id. - Safe from SQL injections.
- 3-my_safe_filter_states.py: Python script
that uses MySQLdb to display all values matching a given name in the
-
4. Cities by states
- 4-cities_by_state.py: Python script that uses
MySQLdb to list all cities from the database
hbtn_0e_4_usa. - Usage:
./4-cities_by_state.py <mysql username> <mysql password> <database name>. - Results are ordered by ascending
cities.id.
- 4-cities_by_state.py: Python script that uses
MySQLdb to list all cities from the database
-
5. All cities by state
- 5-filter_cities.py: Python script that uses MySQLdb
to list all cities of a given state in the database
hbtn_0e_4_usa. - Usage:
./5-filter_cities.py <mysql username> <mysql password> <database name>. - Results are sorted by ascending
cities.id.
- 5-filter_cities.py: Python script that uses MySQLdb
to list all cities of a given state in the database
-
6. First state model
- model_state.py: Python module defining a class
Statethat inherits from SQLAlchemyBaseand links to the MySQL tablestates.
- model_state.py: Python module defining a class
-
7. All states via SQLAlchemy
- 7-model_state_fetch_all.py: Python script
that uses SQLAlchemy to list all
Stateobjects from the databasehbtn_0e_6_usa. - Usage:
./7-model_state_fetch_all.py <mysql username> <mysql password> <database name>. - Results are sorted by ascending
states.id.
- 7-model_state_fetch_all.py: Python script
that uses SQLAlchemy to list all
-
8. First state
- 8-model_state_fetch_first.py: Python script
that uses SQLAlchemy to print the first
Stateobject from the databasehbtn_0e_6_usa, ordered bystates.id. - Usage:
./8-model_state_fetch_first.py <mysql username> <mysql password> <database name>. - If the
statestable is empty, printsNothing.
- 8-model_state_fetch_first.py: Python script
that uses SQLAlchemy to print the first
-
9. Contains
a- 9-model_state_filter_a.py: Python script
that uses SQLAlchemy to list all
Stateobjects that contain the letterain the databasehbtn_0e_6_usa. - Usage:
./9-model_state_filter_a.py <mysql username> <mysql password> <database name>. - Results are ordered by ascending
states.id.
- 9-model_state_filter_a.py: Python script
that uses SQLAlchemy to list all
-
10. Get a state
- 10-model_state_my_get.py: Python script that
uses SQLAlchemy to print the
idof theStateobject with name matching that passed as argument in the databasehbtn_0e_6_usa. - Usage:
./10-model_state_my_get.py <mysql username> <mysql password> <database name> <state searched name>. - Displays the
idof the matchedState. - If no match is found, prints
Not found.
- 10-model_state_my_get.py: Python script that
uses SQLAlchemy to print the
-
11. Add a new state
- 11-model_state_insert.py: Python script that
uses SQLAlchemy to add the
Stateobject "Louisiana" to the databasehbtn_0e_6_usa. - Usage:
./11-model_state_insert.py <mysql username> <mysql password> <database name>. - Prints the
idof the newStateafter creation.
- 11-model_state_insert.py: Python script that
uses SQLAlchemy to add the
-
12. Update a state
- 12-model_state_update_id_2.py: Python
script that uses SQLAlchemy to change the name of the
Stateobject withid = 2in the databasehbtn_0e_6_usato "New Mexico". - Usage:
./12-model_state_update_id_2.py <mysql username> <mysql password> <database name>.
- 12-model_state_update_id_2.py: Python
script that uses SQLAlchemy to change the name of the
-
13. Delete states
- 13-model_state_delete_a.py: Python script
that uses SQLAlchemy to delete all
Stateobjects with a name containing the letterafrom the databasehbtn_0e_6_usa. - Usage:
./13-model_state_delete_a.py <mysql username> <mysql password> <database name>.
- 13-model_state_delete_a.py: Python script
that uses SQLAlchemy to delete all
-
14. Cities in state
- model_city.py: Python module defining a class
Citythat inherits from SQLAlchemyBaseand links to the MySQL tablecities.- Includes class attribute
state_idthat is a foreign key tostates.id.
- Includes class attribute
- 14-model_city_fetch_by_state.py:
Python script that uses SQLAlchemy to list all
Cityobjects in the databasehbtn_0e_14_usa. - Usage:
./14-model_city_fetch_by_state.py <mysql username> <mysql password> <database name>. - Results are sorted by ascending
cities.id.
- model_city.py: Python module defining a class
-
15. City relationship
- relationship_state.py: Python module defining a
class
Statethat inherits from SQLAlchemyBaseand links to the MySQL tablestates.- Identical to the
Stateclass defined in model_state.py. - Includes class attribute
classesthat represents a relationship with the classCity. If theStateobject is deleted, all linkedCityobjects are also deleted.Stateobjects are backreferenced toCityobjects asstate.
- Identical to the
- relationship_city.py: Python module defining a
class
Citythat inherits from SQLAlchemyBaseand links to the MySQL tablecities.- Identical to the
Cityclass defined in model_city.py.
- Identical to the
- 100-relationship_states_cities.py:
Python script that uses SQLAlchemy to add the
State"California" withCity"San Francisco" to the databasehbtn_0e_100_usa. - Usage:
./100-relationship_states_cities.py <mysql username> <mysql password> <database name>. - Uses the
citiesrelationship for allStateobjects.
- relationship_state.py: Python module defining a
class
-
16. List relationship
- 101-relationship_states_cities_list.py:
Python script that uses SQLAlchemy to list all
Stateand correspondingCityobjects in the databasehbtn_0e_101_usa. - Usage:
./101-relationship_states_cities_list.py <mysql username> <mysql password> <database name>. - Uses the
citiesrelationship for allStateobjects. - Results are sorted by ascending
states.idandcities.id.
- 101-relationship_states_cities_list.py:
Python script that uses SQLAlchemy to list all
-
17. List city
- 102-relationship_cities_states_list.py:
Python script that uses SQLAlchemy to list all
Cityobjects from the databasehbtn_0e_101_usa. - Usage:
./102-relationship_cities_states_list.py <mysql username> <mysql password> <database name>. - Uses the
staterelationship to access theStateobjects linked toCityobjects. - Results are sorted by ascending
cities.id.
- 102-relationship_cities_states_list.py:
Python script that uses SQLAlchemy to list all
-
Prince Solomon princexz
All work contained in this project was completed as part of the curriculum for ALX Africa SE. ALX Africa is an online full-stack software engineering program that prepares students for careers in the tech industry using project-based peer learning. For more information, visit this link.