Python Forum
flask-SQLAlchemy query with keyword as function argument
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
flask-SQLAlchemy query with keyword as function argument
#1
Hi,
I want to make the GPIO change and store the state in a database. I am stuck with one part where in the tutorial I am following he is using a dictionary instead of a database. In his code he lists the pins with number name and state, I have done the same in a class, this is his code I need to convert to a sql query; pins[changePin]['name'], where pins is the dictionary name, "changePin" is the function argument replacing the name of the dictionary pin number and 'name' is the name given to the pin. So here is some of his code;tutorial
pins = {
   24 : {'name' : 'coffee maker', 'state' : GPIO.LOW},
   25 : {'name' : 'lamp', 'state' : GPIO.LOW}
@app.route("/<changePin>/<action>")
def action(changePin, action):
   # Convert the pin from the URL into an integer:
   changePin = int(changePin)
   # Get the device name for the pin being changed:
   deviceName = pins[changePin]['name']
 
my equivilant code is;
@bp.route("/<changePin>/<action>")
def action(changePin, action):
    pins = Pins.query.all()
    pin = changePin
    deviceName = Pins.query.filter(pin=pin)
#the database model is;
class Pins(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    pin = db.Column(db.Integer, index=True, unique=True)
    name = db.Column(db.String(64))
    upDown = db.Column(db.String(4))
    state = db.Column(db.String(9))
    
    def __repr__(self):
        return '<Valves {}>'.format(self.pin)    
the class is in a seperate file. The action is carried out on a web page and the device name is displayed on the page as a message showing the change.
My question is "How do I use a function argument to replace a variable when quering the database.
Does this make sense?
I have tried a multitude of different wys of presenting this query, but each presents its own error message.
I now understand the difference between filter and filter_by ie (pins = '5') filters the pin column and presents the row for this pin but how do I replace the '5' with "changePin", If I were to list all the different ways I have tried we would be on the second page now! Smile
Regards
Paul
Reply
#2
Hi all.
I am sorry but I asked this question on another forum yesterday (a cardinal sin) and somebody has helped me. Its the first time anybody has helped me on that forum. any way the answer to my problem is:
    
    rows = Pins.query.filter_by(pin=changePin).all()
    deviceName = rows[0].name
It works but I dont understand how the "rows[0].name" works for all rows (ie if I call pin 23 name for 23 is displayed) Is "rows[0].name" a dictionary produced by
rows = Pins.query.filter_by(pin=changePin).all()
There is so much to learn!!
I love python
Reply
#3
I'd guess its the Pins.query.filter which is filtering your records (like a where clause in the sql).
[0] just points to the first returned record... assuming it exists.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Flask: sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) database is locked pythonpaul32 1 2,029 Apr-04-2023, 07:44 AM
Last Post: Larz60+
  Flask error sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) UNIQUE constraint pythonpaul32 2 3,494 Feb-21-2023, 03:13 AM
Last Post: noisefloor
  Flask and SQLAlchemy question: Database is being created but tables aren't adding pythonpaul32 3 4,415 Feb-07-2023, 10:48 AM
Last Post: pythonpaul32
  Flask run function in background and auto refresh page raossabe 2 7,256 Aug-20-2022, 10:00 PM
Last Post: snippsat
Shocked Django __init__() got an unexpected keyword argument 'any' ikurorox 2 5,384 Nov-26-2021, 07:57 PM
Last Post: ikurorox
  IndexError: list index out of range" & "TypeError: The view function f: Flask Web App joelbeater992 5 3,456 Aug-31-2021, 08:08 PM
Last Post: joelbeater992
  Error updating one to many relationship in Flask/ SQLAlchemy atindra 0 3,300 Apr-15-2021, 10:29 PM
Last Post: atindra
  Flask migrate sqlalchemy not found TomasAm 2 3,449 Dec-01-2020, 10:04 AM
Last Post: TomasAm
  how to pass javascript variables to url_for function in a flask template experimental 5 6,290 Oct-29-2020, 03:29 AM
Last Post: universe
  python 3.7 on windows using flask and flask-sqlalchemy. Alpy 2 3,945 Aug-12-2020, 07:24 PM
Last Post: Alpy

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020