S
S
Shandy2021-10-12 23:01:25
Python
Shandy, 2021-10-12 23:01:25

How to insert a variable into a PyMySQL string?

I insert data into the table like this:

cur.execute("INSERT INTO users_data (`user_id`, `name`) VALUES (%s, 'Канал %s');", (member.id, member.display_name,))

The problem is that the output is not 'Канал User_name', but 'Канал 'User_name''. It throws an error
Mistake
pymysql.err.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'User_name'')' at line 1")

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
galaxy, 2021-10-13
@MrShandy

Form the desired value in the parameters. Quote PyMySQL itself:

cur.execute("INSERT INTO users_data (`user_id`, `name`) VALUES (%s, %s);", (member.id, 'Канал {}'.format(member.display_name))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question