修改字典中的值,存储在SQLite数据库中(Flask Python)



我有一个使用SQLite数据库的Flask应用程序。我创建了这个模型来加载数据到db

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

这个数据库已经填充了大量的信息,我正在尝试更改存储信息中的字典中的特定变量。

# 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']}]]

具体来说,我试图改变'test1'中的'b',所以我尝试了这个:

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

但是,这不能改变任何东西。

代码只改变变量xLoaded的值,不改变变量x的值

修改变量x的值

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

最新更新