最新消息:20190717 VPS服务器:Vultr新加坡,WordPress主题:大前端D8,统一介绍入口:关于

[已解决]Flask中sqlalchemy操作数据库如何删除 +查询 元素

Flask crifan 1010浏览 0评论

搜:

Flask sqlalchemy db delete

Select, Insert, Delete — Flask-SQLAlchemy Documentation (2.1)

选择(Select),插入(Insert), 删除(Delete) — Flask-SQLAlchemy 2.0 documentation

db.session.delete(me)

但是,直接额外生成一个对象,再去

db.session.delete(someUser)

是不行的。

最后是用:

#!flask/bin/python
# -*- coding: UTF-8 -*-
# from migrate.versioning import api
# from config import SQLALCHEMY_DATABASE_URI
# from config import SQLALCHEMY_MIGRATE_REPO
# import os.path
# db.create_all()
# if not os.path.exists(SQLALCHEMY_MIGRATE_REPO):
#     api.create(SQLALCHEMY_MIGRATE_REPO, ‘database repository’)
#     api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
# else:
#     api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO, api.version(SQLALCHEMY_MIGRATE_REPO))
from sipevents import db
from sipevents import app
from sipevents import models
from sipevents import User
db.create_all()
crifanUserJson = {
  “province”: u”江苏”,
  “openid”: “oswjmv4X0cCXcfkIwjoDfCkeTVVY”,
  “headimgurl”: “http://wx.qlogo.cn/mmopen/ajxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxFQ/0”,
  “language”: u”zh_CN”,
  “city”: u”苏州”,
  “country”: u”中国”,
  “sex”: 1,
  “privilege”: [],
  “nickname”: u”礼貌”
}
app.logger.debug(‘crifanUserJson=%s’, crifanUserJson)
crifanUser = User(openid = crifanUserJson[‘openid’],
                  province = crifanUserJson[‘province’],
                  headimgurl = crifanUserJson[‘headimgurl’],
                  language = crifanUserJson[‘language’],
                  city = crifanUserJson[‘city’],
                  country = crifanUserJson[‘country’],
                  sex = crifanUserJson[‘sex’],
                  nickname = crifanUserJson[‘nickname’])
app.logger.debug(‘crifanUser=%s’, crifanUser)
db.session.add(crifanUser)
db.session.commit()
app.logger.debug(‘added crifanUser=%s’, crifanUser)
currentUsers = User.query.all()
app.logger.debug(‘currentUsers=%s’, currentUsers)
currentUsersCount = len(currentUsers)
app.logger.debug(‘currentUsersCount=%s’, currentUsersCount)
if currentUsersCount > 0  :
    firstUser = currentUsers[0]
    app.logger.debug(‘firstUser=%s’, firstUser)
    db.session.delete(firstUser)
db.session.commit()
    app.logger.debug(‘deleted firstUser=%s’, firstUser)

才可以正常运行。

关于查询,后来去用:

#!flask/bin/python
# -*- coding: UTF-8 -*-
# from migrate.versioning import api
# from config import SQLALCHEMY_DATABASE_URI
# from config import SQLALCHEMY_MIGRATE_REPO
# import os.path
# db.create_all()
# if not os.path.exists(SQLALCHEMY_MIGRATE_REPO):
#     api.create(SQLALCHEMY_MIGRATE_REPO, ‘database repository’)
#     api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
# else:
#     api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO, api.version(SQLALCHEMY_MIGRATE_REPO))
from sipevents import db
from sipevents import app
from sipevents import models
from sipevents import User
db.create_all()
crifanUserJson = {
  “province”: u”江苏”,
  “openid”: “oswjmv4X0cCXcfkIwjoDfCkeTVVY”,
  “headimgurl”: “http://wx.qlogo.cn/mmopen/ajxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxFQ/0”,
  “language”: u”zh_CN”,
  “city”: u”苏州”,
  “country”: u”中国”,
  “sex”: 1,
  “privilege”: [],
  “nickname”: u”礼貌”
}
app.logger.debug(‘crifanUserJson=%s’, crifanUserJson)
existedUser = User.query.filter_by(openid=crifanUserJson[‘openid’]).first()
if existedUser :
    # has exsited this user
    # callbackOkStr = u’已存在此用户 existedUser=%r’ % (existedUser)
    app.logger.debug(u’已存在此用户 openid=%s, nickname=%s’, crifanUserJson[‘openid’], crifanUserJson[‘nickname’])
else :
    crifanUser = User(openid = crifanUserJson[‘openid’],
                      province = crifanUserJson[‘province’],
                      headimgurl = crifanUserJson[‘headimgurl’],
                      language = crifanUserJson[‘language’],
                      city = crifanUserJson[‘city’],
                      country = crifanUserJson[‘country’],
                      sex = crifanUserJson[‘sex’],
                      nickname = crifanUserJson[‘nickname’])
    app.logger.debug(‘crifanUser=%s’, crifanUser)
    db.session.add(crifanUser)
db.session.commit()
    app.logger.debug(‘added crifanUser=%s’, crifanUser)
currentUsers = User.query.all()
app.logger.debug(‘currentUsers=%s’, currentUsers)
currentUsersCount = len(currentUsers)
app.logger.debug(‘currentUsersCount=%s’, currentUsersCount)
if currentUsersCount > 0  :
    firstUser = currentUsers[0]
    app.logger.debug(‘firstUser=%s’, firstUser)
    db.session.delete(firstUser)
db.session.commit()
    app.logger.debug(‘deleted firstUser=%s’, firstUser)

转载请注明:在路上 » [已解决]Flask中sqlalchemy操作数据库如何删除 +查询 元素

发表我的评论
取消评论

表情

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

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
56 queries in 0.261 seconds, using 18.92MB memory