最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]SQLAlchemy中update已有的某列的信息

SQLAlchemy crifan 6572浏览 0评论

折腾:

[已解决]SQLAlchemy中commit出错:IntegrityError sqlite3.IntegrityError column openid is not unique SQL

期间,对于已有一个User的数据,现在当获得新的数据时,需要去update已有的信息

SQLAlchemy参考 – YS.Zou

session.query(User).filter(User.username == ‘abc’).update({‘name’: ‘123’})
session.commit()
user = session.query(User).filter_by(username=’abc’).scalar()
user.name = ‘223’
session.commit()

SQLAlchemy update

Insert, Updates, Deletes — SQLAlchemy 1.1 Documentation

stmt = users.update().\
            where(users.c.id==5).\
            values(name=’user #5′)

python – How to update SQLAlchemy row entry? – Stack Overflow

“user.no_of_logins += 1

session.commit()”

flask-SQLAlchemy 的 update 操作是怎么用的呢? – V2EX

python – Efficiently updating database using SQLAlchemy ORM – Stack Overflow

最后是用:

def getUserFromDb(curUserOpenid):
    existedUser = User.query.filter_by(openid=curUserOpenid).first()
    gLog.debug(‘existedUser=%s’, existedUser)
    return existedUser
existedUser = getUserFromDb(userOpenid)
gLog.debug(‘existedUser=%s’, existedUser)
if existedUser:
    if (province) and (province != existedUser.province):
        gLog.debug(“need update province from %s to %s”, existedUser.province, province)
        existedUser.province = province
    if (avatarUrl) and (avatarUrl != existedUser.avatar_url):
        gLog.debug(“need update avatar_url from %s to %s”, existedUser.avatar_url, avatarUrl)
        existedUser.avatar_url = avatarUrl
    if (avatarStaticPath) and (avatarStaticPath != existedUser.avatar_static_path):
        gLog.debug(“need update avatar_static_path from %s to %s”, existedUser.avatar_static_path, avatarStaticPath)
        existedUser.avatar_static_path = avatarStaticPath
    if (language) and (language != existedUser.language):
        gLog.debug(“need update language from %s to %s”, existedUser.language, language)
        existedUser.language = language
    if (city) and (city != existedUser.city):
        gLog.debug(“need update city from %s to %s”, existedUser.city, city)
        existedUser.city = city
    if (country) and (country != existedUser.country):
        gLog.debug(“need update country from %s to %s”, existedUser.country, country)
        existedUser.country = country
    if (sex) and (sex != existedUser.sex):
        gLog.debug(“need update sex from %s to %s”, existedUser.sex, sex)
        existedUser.sex = sex
    if (nickname) and (nickname != existedUser.nickname):
        gLog.debug(“need update nickname from %s to %s”, existedUser.nickname, nickname)
        existedUser.nickname = nickname
db.session.commit()
    gLog.debug(“updated existedUser=%s”, existedUser)
else:
    newUser = User( openid = userOpenid,
                    province = province,
                    avatar_url = avatarUrl,
                    avatar_static_path = avatarStaticPath,
                    language = language,
                    city = city,
                    country = country,
                    sex = sex,
                    nickname = nickname)
    gLog.debug(‘newUser=%s’, newUser)
    db.session.add(newUser)
db.session.commit()
    gLog.debug(‘added newUser=%s’, newUser)
curUser = getUserFromDb(userOpenid)
gLog.debug(‘found curUser=%s’, curUser)

即可:

去更新对应的User的信息了。

[总结]

如果想要更新一个table中的某Column,则可以:

在得到对应的对象的时候,直接设置新的值,然后再去commit即可。

比如:

existedUser = User.query.filter_by(openid=curUserOpenid).first()
existedUser.nickname = newNickname
db.session.commit()

即可实现update的操作。

转载请注明:在路上 » [已解决]SQLAlchemy中update已有的某列的信息

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
89 queries in 0.155 seconds, using 22.10MB memory