Create a table to established connection with SQLAlchemy

Create a Test Table then Insert data to establish a connection

Situation: You want to create a quick ‘test’ table via the sqlalchemy library in Python to establish a connection with your postgres db.

# Create TEST table to confirm connection
db.execute(
    "CREATE TABLE IF NOT EXISTS films (title text, director text, year text)")

# Insert data
db.execute(
    "INSERT INTO films (title, director, year) VALUES ('Dune', 'Denis Villeneuve', '2021')")

# Read data
result_set = db.execute("SELECT * FROM films")
for r in result_set:
    print(r)

For more content on data science, R, and Python find me on Twitter.

Previous
Next