pandaZW pandaZW - 9 months ago 60
Python Question

How to insert ' into postgres?

Here i want insert a name into pg.
Sometimes,this name is not regular like the following picture.

How can i update this data into db?

Answer Source

You should use query parameters. In Sqlite you would do something like:

sql_insert = 'INSERT INTO articles VALUES(?, ?)'
to_db = [authors, link]
curs.execute(sql_insert, to_db)

or to update:

sql_update = 'UPDATE articles SET authors = ? WHERE link = ?'
to_db = [authors, link]
curs.execute(sql_update, to_db)

to_db is a list of parameter that you pass, instead of using string manipulation through %s as comment of @Colonel Thirty Two suggested.