简体   繁体   中英

postgresql sql query syntax error at or near “convert_from” in python

i think this is easy question, how to write sql query in python?

### python3 code
# ok 
row = await conn.fetchrow(
    'SELECT * FROM users WHERE email = $1', user_email)

# NOT ok
user_pw = await conn.fetch(                                                          
    'convert_from($1, $2)', row[2], 'UTF8') 

asyncpg.exceptions.PostgresSyntaxError: syntax error at or near "convert_from"

i had fixed my bug with following code:

tmp_pw = await conn.fetch(
    'SELECT * FROM convert_from($1, $2)', row[2], 'UTF8')

# because, tmp_pw is  
#[<Record convert_from='$2a$12$PsXD/oaDXwC1Ffup842I/emQ3Bz26AWjQNsLBQipQ4gvRIFhUQAXb2'>]
# any suggestion is welcome.
user_pw = str(tmp_pw).strip('[]').strip('<>').split('=')[1].strip('\'')

# another method to get pw from Record (not tested):
# tmp_iter = iter(tmp_pw)
# user_pw = next(tmp_iter)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM