Last week we created candidates and companies databases using SQL for our Tech Talent recruiting firm. This week, we'll be taking that file and converting it into a Python file using the sqlite3 module. You don't need to install sqlite3 separately as it comes pre-installed with Python! The values that will be stored in each of these tables are represented in the entity diagrams shown below (this is the same as last week).
By the end of this code along, our recruiting firm will be able to store information in a Python file so that it can be manipulated and utilized as part of a broader program.
This week’s code utilizes SQLite3 to implement the commands. This should have been installed last week, but here are the directions again if needed:
- Windows: Follow these directions.
- Apple: Nothing at all! SQLite comes installed on all Macs. If for some reason you don’t have it, you can download it using Homebrew.
- Linux: Follow these directions.
- Create a file called
tech_talent.db. - The remainder of this code along can be done in
tech_talent.py. At the top of the file, import thesqlite3module. - Create a connection variable and call
.connect()to connect to thetech_talent.dbfile. - Create a cursor by calling
.cursor()on the connection you created the previous step. - The
companiesandcandidatestables may already exist in the database. Using.execute(), call theDROP TABLE IF EXISTSSQL command. - Create the
companiesandcandidatestables using.execute()andCREATE TABLE. - Run the tests! The
test_companies_table_exists,test_candidates_table_exists,test_companies_table_columns, andtest_candidates_table_columnstests should now be passing. - Insert values below into the
companiesandcandidatestables using.execute()andINSERT INTO...VALUES.
company_name |
industry |
location |
|---|---|---|
| TechCorp | Software | San Francisco |
| DataDynamics | Data Analytics | Boston |
| CloudNine | Cloud Computing | Seattle |
first_name |
last_name |
email |
years_experience |
primary_skill |
|---|---|---|---|---|
| John | Smith | john.smith@email.com | 5 | Python |
| Sarah | Johnson | sarah.j@email.com | 3 | Data Science |
| Michael | Lee | michael.lee@email.com | 7 | Cloud Architecture |
| Emma | Wilson | emma.w@email.com | 2 | Frontend Development |
| James | Brown | james.b@email.com | 4 | Python |
- Commit the transaction using the command
connection.commit() - Using a
SELECTcommand and.execute()get the data stored in each table. Call.fetchall()on the returned values. Print out the values in each table. - Print out the 2nd and 3rd candidates that were returned.
- What data type is in this list? Use
type()command on the data that was returned to show that it is a tuple. - We can access values the exact same way we access values in a list. Print out the 2nd candidates name to show this.
- Create a SQL query so that you can print out only candidates that have a primary skill of
"Python". Save the values that are returned in the variable calledpython_candidates. - Iterate through the data you got in the last step and print out candidates email that have Python has a skill.
- Run the tests! The
test_python_candidates_genretest should now be passing. - Create a SQL query so that you can print out only the
companiesname that are in anindustryof"Software". - Create a SQL query so that you can print out only those
candidatesname and primary skill that have more than 3 years of experience. - Create a SQL query to update John's email to the correct value of
john.smith@gmail.com. Don't forget to commit these changes! - Update the candidate with a
candidate_idof 5 wants to update theirprimary_skillto bePython, SQL. Don't forget to commit these changes! - Run the tests!
test_email_updateandtest_candidate5_updateshould now be passing. - Delete the 'Cloud Nine' row from the
companiestable. - Delete all
candidatesthat have aprimary_skillof"Cloud Architecture". Don't forget to commit the changes! - Run the tests!
test_cloud_nine_deleted,test_cloud_architecture_deleted, andtest_final_tablesshould now be passing. - Print out the remaining values in the
candidatestable. - Close the connection
