Creating a Connection
Creating a Connection#
import pandas as pd
import numpy as np
from sqlalchemy import create_engine
import sqlite3
We start by creating a connection to the database. The code below doesn’t actually connect just yet – we’re just creating the engine with which we will connect to the database.
conn = sqlite3.connect("../../lodes.db")
Similar to when we read in CSV files, we’re just pointing to a file path containing our database, lodes.db
. It’s actually located in a different location than this notebook if you opened this in Binder, so that is why it isn’t just the name of the database. If you opened this notebook using another method, you will need to make sure the path above points to where the database is located.
If we were to use a different flavor of SQL (such as PostgreSQL), then we would create our connection to the database slightly differently. For example, we could use the create_engine
function inside the psycopg2
package to connect to a PostgreSQL database. After you create the connection, though, everything afterwards is the same – you can use that connection to write SQL code to do all the same things such as bringing in a table as a DataFrame.