gpt4 book ai didi

python - Flask-SQLAlchemy 检查行是否存在

转载 作者:行者123 更新时间:2023-12-01 01:27:39 24 4
gpt4 key购买 nike

我已经看过 this所以问题,到目前为止还没有帮助。

因此,我尝试使用 Flask-SQLAlchemy 检查表中是否存在一行,但到目前为止没有任何帮助。

我当前的代码:

@app.route('/guilds/<guildid>')
def guildsettings(guildid):
discord = make_session(token=session.get('oauth2_token'))
user = discord.get(API_BASE_URL + '/users/@me').json()
guilds = discord.get(API_BASE_URL + '/users/@me/guilds').json()
row = Settings.query.all()
exists = Settings.query.filter_by(db.exists().where(Settings.guildid==guildid)).scalar()
print(exists)
if exists == True:
info = Settings.query.filter_by(guildid=guildid).first()
return render_template("guild.html", id=guildid, guilds=guilds, User=user, prefix=info.prefix, logschannel=info.logschannel, modrole=info.modrole, adminrole=info.adminrole, welcomechannel=info.welcomechannel, welcomemessage=info.welcomemessage, dadbot=info.dadbot, music=info.music, funcmds=info.funcmds, weather=info.weather, wapikey=info.wapikey)
else:
return render_template("notinserver.html", guildid=guildid, link="https://discordapp.com/api/oauth2/authorize?client_id=xxxxxx&permissions=8&redirect_uri=xxxxxxx%2Fme&scope=bot&guild_id={}".format(guildid))

我不知道现在该怎么办。如有任何帮助,我们将不胜感激。

最佳答案

如果它需要恰好是一个结果:

try:
Settings.query.filter_by(db.exists().where(Settings.guildid==guildid)).one()
except sqlalchemy.orm.exc.NoResultFound:
return False
except sqlalchemy.orm.exc.MultipleResultsFound:
#do what ever you want to do if there is more than one result
return True

关于python - Flask-SQLAlchemy 检查行是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53210580/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com