Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Changing value in dictionary, stored in SQLite database (Flask Python)

I have a Flask application that is using SQLite database. I have created this model to load data into the db

class Category(db.Model, UserMixin):
    id = db.Column(db.Integer, primary_key=True)
    products = db.Column(db.Text)

This database has been populated with a lot of information, and I am trying to change a specific variable in a dictionary within the stored information.

# print 'products' for the first Category stored

x = Category.query.all()
xLoaded = json.loads(x[0].products)
print(xLoaded)

# output
[['test1', {'path': ['a', 'b', 'c']}], ['test2', {'path': ['d', 'e', 'f']}]]

Specifically, I am trying to change 'b' in 'test1' so I tried this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

x = Category.query.all()
xLoaded = json.loads(x[0].products)
xLoaded[0][1]['path'][1] = 'z'

db.session.commit()

However, this fails to change anything.

>Solution :

The code only changes the value of the variable xLoaded not the x.

Change the value of variable x

x = Category.query.all()
xLoaded = json.loads(x[0].products)
xLoaded[0][1]['path'][1] = 'z'
x[0].products = xLoaded

db.session.commit()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading