Simple SELECT statement on existing table with SQLAlchemy

Issue

Nowhere on the internet does there exist a simple few-line tutorial on a simple SELECT statement for SQLAlchemy 1.0.

Assuming I’ve established my database connection using create_engine(), and my database tables already exist, I’d like to know how to execute the following query:

select
    name,
    age
from
    users
where
    name = 'joe'
    and
    age = 100

Solution

Found this while trying to figure out the same thing.

To select data from a table via SQLAlchemy, you need to build a representation of that table within SQLAlchemy. If Jupyter Notebook’s response speed is any indication, that representation isn’t filled in (with data from your existing database) until the query is executed.

You need Table to build a table. You need select to select data from the database. You need metadata… for reasons that aren’t clear, even in the docs.

from sqlalchemy import create_engine, select, MetaData, Table, and_

engine = create_engine("dburl://user:[email protected]/schema")
metadata = MetaData(bind=None)
table = Table(
    'table_name', 
    metadata, 
    autoload=True, 
    autoload_with=engine
)

stmt = select([
    table.columns.column1,
    table.columns.column2
]).where(and_(
    table.columns.column1 == 'filter1',
    table.columns.column2 == 'filter2'
))

connection = engine.connect()
results = connection.execute(stmt).fetchall()

You can then iterate over the results. See SQLAlchemy query to return only n results? on how to return one or only a few rows of data, which is useful for slower/larger queries.

for result in results:
    print(result)

I checked this with a local database, and the SQLAlchemy results are not equal to the raw SQL results. The difference, for my data set, was in how the numbers were formatted. SQL returned float64 (e.g., 633.07), while SQLAlchemy returned objects (I think Decimal, e.g. 633.0700000000.)

Some help from DataCamp’s Introduction to Databases in Python

Answered By – Evan

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published