1. Basics of Relational Databases - upalr/Python-camp GitHub Wiki
1 Introduction to Databases
1.1 A database consists of tables
1
1.1 Table consist of columns and rows
2
1.3 Tables can be related
3
2 Connecting to your Database
2.1 Meet SQLAlchemy
4
2.2 There are many types of databases
5
2.3 Connecting to a database
Alright, it's time to create your first engine! An engine is just a common interface to a database, and the information it requires to connect to one is contained in a connection string, such as sqlite:///census_nyc.sqlite. Here, sqlite is the database driver, while census_nyc.sqlite is a SQLite file contained in the local directory.
6
It is worth noting SQLAlchemy won't actually make the connection untill we give it some work to execute
Note that when you just want to print the table names, you do not need to use engine.connect() after creating the engine.
2.4 A word on connection strings
7
2.5 What's in your database?
8
2.6 Reflection**
SQLAlchemy can be used to automatically load tables from a database using something called reflection. Reflection is the process of reading the database and building the metadata based on that information. It's the opposite of creating a Table by hand and is very useful for working with existing databases.
9
2.7 Example : Viewing Table Details
Great job reflecting the census table! Now you can begin to learn more about the columns and structure of your table. It is important to get an understanding of your database by examining the column names. This can be done by using the .columns attribute and accessing the .keys() method. For example, census.columns.keys() would return a list of column names of the census table.
Following this, we can use the metadata container to find out more details about the reflected table such as the columns and their types. For example, table objects are stored in the metadata.tables dictionary, so you can get the metadata of your census table with metadata.tables['census']. This is similar to your use of the repr() function on the census table from the previous exercise.
# Reflect the census table from the engine: census
census = Table('census', metadata, autoload=True, autoload_with=engine)
# Print the column names
print(census.columns.keys())
# Print full table metadata
print(repr(metadata.tables['census']))
RESULT 10
3 Introduction to SQL
3.1 SQL Statements
11
3.2 Basic SQL querying
12 13
3.3 ResultProxy vs ResultSet
14
3.4 Handling ResultSets
15
3.4.1 Example: Handling a ResultSet
Recall the differences between a ResultProxy and a ResultSet:
- ResultProxy: The object returned by the
.execute()method. It can be used in a variety of ways to get the data returned by the query. - ResultSet: The actual data asked for in the query when using a fetch method such as
.fetchall()on a ResultProxy. - This separation between the ResultSet and ResultProxy allows us to fetch as much or as little data as we desire.
Once we have a ResultSet, we can use Python to access all the data within it by column name and by list style indexes. For example, you can get the first row of the results by using results[0]. With that first row then assigned to a variable first_row, you can get data from the first column by either using first_row[0] or by column name such as first_row['column_name']. You'll now practice exactly this using the ResultSet you obtained from the census table in the previous exercise. It is stored in the variable results. Enjoy!
# Get the first row of the results by using an index: first_row
first_row = results[0]
# Print the first row of the results
print(first_row)
# Print the first column of the first row by using an index
print(first_row[0])
# Print the 'state' column of the first row by using its name
print(first_row['state'])
3.5 SQLAlchemy to Bulid Queries
Excellent work so far! It's now time to build your first select statement using SQLAlchemy. SQLAlchemy provides a nice "Pythonic" way of interacting with databases. So rather than dealing with the differences between specific dialects of traditional SQL such as MySQL or PostgreSQL, you can leverage the Pythonic framework of SQLAlchemy to streamline your workflow and more efficiently query your data. For this reason, it is worth learning even if you may already be familiar with traditional SQL.
16
3.6 SQLAlchemy quering
17
3.7 SQLAlchemy Select Statement
18