How can I update a row’s information?
For example I’d like to alter the name column of the row that has the id 5.
Advertisement
Answer
Retrieve an object using the tutorial shown in the Flask-SQLAlchemy documentation. Once you have the entity that you want to change, change the entity itself. Then, db.session.commit()
.
For example:
JavaScript
x
8
1
admin = User.query.filter_by(username='admin').first()
2
admin.email = 'my_new_email@example.com'
3
db.session.commit()
4
5
user = User.query.get(5)
6
user.name = 'New Name'
7
db.session.commit()
8
Flask-SQLAlchemy is based on SQLAlchemy, so be sure to check out the SQLAlchemy Docs as well.