Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I select all rows with sqlalchemy?

I am trying to get all rows from a table.

In controller I have:

meta.Session.query(User).all() 

The result is [, ], but I have 2 rows in this table.

I use this model for the table:

import hashlib import sqlalchemy as sa from sqlalchemy import orm  from allsun.model import meta  t_user =  sa.Table("users",meta.metadata,autoload=True)  class Duplicat(Exception):     pass class LoginExistsException(Exception):     pass class EmailExistsException(Exception):     pass 

And next, in the same file:

class User(object):     def loginExists(self):         try:             meta.Session                 .query(User)                 .filter(User.login==self.login)                 .one()         except orm.exc.NoResultFound:             pass         else:             raise LoginExistsException()      def emailExists(self):         try:             meta                 .Session                 .query(User)                 .filter(User.email==self.email)                 .one()         except orm.exc.NoResultFound:             pass         else:             raise EmailExistsException()       def save(self):         meta.Session.begin()         meta.Session.save(self)         try:             meta.Session.commit()         except sa.exc.IntegrityError:             raise Duplicat()  orm.mapper(User, t_user) 
like image 593
gummmibear Avatar asked Apr 13 '10 21:04

gummmibear


People also ask

How do I SELECT data 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 does all () return SQLAlchemy?

As the documentation says, all() returns the result of the query as a list.

What does query all () return?

all() will return all records which match our query as a list of objects.


2 Answers

You can easily import your model and run this:

from models import User  # User is the name of table that has a column name users = User.query.all()  for user in users:     print user.name 
like image 123
Nima Soroush Avatar answered Sep 22 '22 12:09

Nima Soroush


I use the following snippet to view all the rows in a table. Use a query to find all the rows. The returned objects are the class instances. They can be used to view/edit the values as required:

from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import create_engine, Sequence from sqlalchemy import String, Integer, Float, Boolean, Column from sqlalchemy.orm import sessionmaker  Base = declarative_base()  class MyTable(Base):     __tablename__ = 'MyTable'     id = Column(Integer, Sequence('user_id_seq'), primary_key=True)     some_col = Column(String(500))      def __init__(self, some_col):         self.some_col = some_col  engine = create_engine('sqlite:///sqllight.db', echo=True) Session = sessionmaker(bind=engine) session = Session()  for class_instance in session.query(MyTable).all():     print(vars(class_instance))  session.close() 
like image 23
Sheece Gardazi Avatar answered Sep 21 '22 12:09

Sheece Gardazi