Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple SELECT statement on existing table with SQLAlchemy

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
like image 387
ryantuck Avatar asked Jun 11 '15 16:06

ryantuck


People also ask

How do I select in SQLAlchemy?

The select() method of table object enables us to construct SELECT expression. The resultant variable is an equivalent of cursor in DBAPI. We can now fetch records using fetchone() method. Here, we have to note that select object can also be obtained by select() function in sqlalchemy.

How do you update existing table rows in SQLAlchemy in Python?

Update table elements in SQLAlchemy. Get the books to table from the Metadata object initialized while connecting to the database. Pass the update query to the execute() function and get all the results using fetchall() function. Use a for loop to iterate through the results.

How fetch data is used in SQLAlchemy?

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.

What is subquery in SQLAlchemy?

The grouping is done with the group_by() query method, which takes the column to use for the grouping as an argument, same as the GROUP BY counterpart in SQL. The statement ends by calling subquery() , which tells SQLAlchemy that our intention for this query is to use it inside a bigger query instead of on its own.


3 Answers

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:pass@database/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

like image 119
Evan Avatar answered Oct 04 '22 21:10

Evan


Since the original question has two columns in the select statement, and it can confuse some people on how to write using that:

from sqlalchemy import and_  
stmt = select([users.columns.name,users.columns.age])  
stmt= stmt.where(and_(name=='joe',age==100)  
for res in connection.execute(stmt):  
    print(res)
like image 33
Smitty Avatar answered Oct 04 '22 21:10

Smitty


Sticking with SQL alchemy for this seems overcomplicated. What you can do instead, is to pass SQL alchemy engine component, to pandas.read_sql(sql,conn). https://pandas.pydata.org/docs/reference/api/pandas.read_sql.html

from sqlalchemy import create_engine
import pandas as pd

engine = create_engine(.....)
sql = "select name, age from users where name = 'joe' and age = 100"
df = pd.read_sql(sql,con=engine)
like image 39
Michal Kovacik Avatar answered Oct 04 '22 21:10

Michal Kovacik