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
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!
Regards
Paul
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!

Regards
Paul