D
D
dragaleff2021-06-23 00:36:02
MySQL
dragaleff, 2021-06-23 00:36:02

FetchAll, how to display a value from MySql and put it in a variable (AioMysql, PyMysql)?

I'm using Python, the script is below:

await cur.execute("SELECT `user_id` FROM `admins` WHERE id = %s", (f'{id}'))
sql = cur.fetchall()
print(sql)


I get the following message at the output:
Future finished result=(406658530,)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
o5a, 2021-06-23
@o5a

You need to use a tuple to pass parameters. Just adding parentheses to a value doesn't make it a tuple, you need to add a comma: (id, )
According to the query, only one value is required, so you can use fetchone (returns the first row of the result).

await cur.execute("SELECT `user_id` FROM `admins` WHERE id = %s", (id, ))
sql = cur.fetchone()
if sql:
    user_id = sql[0]
    print(user_id)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question