A
A
Alex Fedorov2021-06-27 00:29:32
Python
Alex Fedorov, 2021-06-27 00:29:32

Inserting data into Postgresql?

Hello. I'm learning python along with postgre and I want to execute the following query to the database

cursor.execute("INSERT INTO id_list(id) VALUES(?)", user_id)

I read somewhere that values(?) should be done to protect against sql injection , but when I execute
such a query in the postgre base, I get an error. What is the problem ??

When I do this, data is freely added to the table:

cursor.execute("INSERT INTO id_list(id) VALUES(372772)")

This numeric value is added.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
Lev Poznyakov, 2021-06-27
@robben_55

cursor.execute("insert into id_list (id) values (%s)",  (user_id, ))

D
di, 2021-06-28
@Delgus

In postgres $1, $2 are placeholders, not ? like in mysql

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question