Ask coding questions

← Back to all posts
Help with replDB again
h
MathBoy1 (0)

So, I want to check if no two users on my flask site have the same referenceid, and a users db is user = ["refId", "xp"], and the user chooses their own referenceid when signing up... How would I get it to check the refId of all the other existing users?

Comments
hotnewtop
InvisibleOne (3214)

I would do something like this:

from replit import db

keys = db.keys()

all_id = []
for key in keys:
  all_id.append(db[key]["refId"])

But that only works if you are saving the data in a dictionary, if you let me see your code I can probably figure out a better solution.

[deleted]

You could technically set up an SQLite file for that to store the data, and link it to your repl. Or you could create a new file with all the referenceid as a list.

MathBoy1 (0)

@Aphmeta Would there be a direct way to do it through replDB, like traverse through all the users, add their referenceID's to a list, and then check if it is in the list?

MathBoy1 (0)

I don't know how to traverse through the users in the DB though...

[deleted]

@MathBoy1 Yes, but since I am not such an expert, you might want to ask someone else. Sorry, but replDB and flask are not things I usually program in. :-( I'll try to research it though, maybe go to replit docs.

[deleted]

@MathBoy1 I found something that might be useful. https://docs.replit.com/misc/database

[deleted]

@MathBoy1 Ah! Eureka! https://docs.replit.com/tutorials/11-using-the-replit-database
Forget everything I wrote earlier. Only reference the link above.
To find out if someone else already has the refId, then you should make a variable first called refId then do something like:
if refId in db.keys():
print("refId already exists")
Again, look through the repl docs link that I posted above.

The power of the docs!

ruiwenge2 (1135)

@Aphmeta actually it is if refId in db.keys(): print("refId already exists")
because db.keys() returns a list of all the keys in the database

[deleted]

@ruiwenge2 Ah yes

MathBoy1 (0)

@ruiwenge2 But doesn't db.keys() return only the reference, in this case, the user, and not the values inside the users database?

ruiwenge2 (1135)

@MathBoy1 yeah, so you have to get the value by doing value = db[“USER”]