Reputation: 115
app.py:
from flask import Flask, render_template
from itertools import groupby
from flask import request
import MySQLdb
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.ext.automap import automap_base
from sqlalchemy.orm import Session
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from db import PUser
engine = create_engine('mysql://myusername:mypassword@localhost/mydbname')
engine.connect()
Session = sessionmaker(bind=engine)
session = Session()
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://myusername:mypassword@localhost/mydbname'
db = SQLAlchemy(app)
@app.route('/people/')
def people():
result = pUser.query.filter_by(shahr = 'tehran')
result = PUser.query.all()
return result
In the script above, how should I edit these lines to work?
result = PUser.query.filter_by(shahr = 'tehran')
result = PUser.query.all()
I want to select all data from "PUser" table and also know how to filter my select query.
Puser
is the name of a table in my database, and this is a part of db.py in the same directory:
class PUser(Base):
__tablename__ = 'p_user'
Upvotes: 0
Views: 13934
Reputation: 6978
You are making two queries and that's probably why you aren't getting the desired result.
pUser.query
creates a Query
object. On that you're able to call stuff like select_from
, join
, filter
, filter_by
and other methods like that, which also return a Query
object.
To get the resulting rows to a python list
, you have to call all
, one
etc. on that.
So, in your case
result = pUser.query.filter_by(shahr='tehran').all()
Upvotes: 3