Try to search your question here, if you can't find : Ask Any Question Now ?

Change value from SQL command output to match python syntax

HomeCategory: stackoverflowChange value from SQL command output to match python syntax
julie asked 2 weeks ago

So, I’m a newbie regarding SQL and postgresql and database management, but I need a database for a discord bot I’m doing. I’m using asyncpg to manage a database with python

Now, let’s say I have a command wich has to find If username already exists in that database:

try:
            with open("creds.json", "r") as file:  
                creds = json.load(file)
            print("n" + "Connecting to database")    
            host = creds["DB_HOST"]
            user = creds["DB_USER"]
            password = creds["DB_TOKEN"]
            database = creds["DB_NAME"]
            conn = await asyncpg.connect(host=host, user=user, password=password, database=database)
            print("n"+ f"PostgreSQL conection to database stablished succesfully")
            exists = await conn.fetch('''
SELECT EXISTS (
SELECT 1 
FROM moderation 
WHERE username=($1)); 
''', str(member)) 
            await conn.close()  
            print("Query finished, connection to database closed")        
            print(exists)

It either returns True or False in this form [<Record exists=False>] or [<Record exists=True>], so to check if its true or false I have to convert the ‘exists’ to a string and then compare it to either [<Record exists=False>] or [<Record exists=False>] as a string:

if str(exists) == "[<Record exists=False>]":
    ...
elif str(exists) == "[<Record exists=True>]"
    ...

Is there anyway to ‘convert’ [<Record exists=False>] to a plain False and [<Record exists=False>] to a plain True that Python can directly understand?

same goes when getting a value from a table, say, and int that have to be adressed the same way, returning, for example,

[<Record rowname=5>]
1 Answers
Best Answer
Amit answered 2 weeks ago
Your Answer

20 + 20 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com