Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
529 views
in Technique[技术] by (71.8m points)

python - Searching and bringing back data from a DB

So right now I am trying to make a command where it retrieves data on a specific blacklisted user. My current solution is to loop through all the fields until it gets a match and then brings back all of the data regarding the user. The only problem is that my for loop doesn't define q properly causing the last try statement to fail. I suspect the problem is either with my for loop or my try statement. Is there another solution to this?

@commands.command()
async def DBget(self, ctx, *, string: str):
        database.db.connect(reuse_if_open=True)
        databaseData = [database.MRP_Blacklist_Data.DiscUsername, database.MRP_Blacklist_Data.DiscID, database.MRP_Blacklist_Data.Gamertag, database.MRP_Blacklist_Data.BannedFrom, database.MRP_Blacklist_Data.KnownAlts, database.MRP_Blacklist_Data.ReasonforBan, database.MRP_Blacklist_Data.DateofIncident, database.MRP_Blacklist_Data.TypeofBan, database.MRP_Blacklist_Data.DatetheBanEnds]
        for data in databaseData:
            try:
                q: database.MRP_Blacklist_Data = database.MRP_Blacklist_Data.select().where(data == string).get()
            except:
                continue
            else:
                break

        try:
            await ctx.send(f"{q.DiscUsername}
{q.DiscID}
{q.Gamertag}
{q.BannedFrom}
{q.BanReason}
{q.IncidentDate}
{q.BanType}
{q.ExpireBan}")
        except Exception as e:
            await ctx.send(f"ERROR!
{e}")

Traceback: local variable 'q' referenced before assignment

Any help would be appreciated!

question from:https://stackoverflow.com/questions/65852426/searching-and-bringing-back-data-from-a-db

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...