python-cs50 icon indicating copy to clipboard operation
python-cs50 copied to clipboard

Inserting url encoded in postgreSQL adds extra '%' symbols

Open DLAcoding opened this issue 3 years ago • 0 comments

I have noticed that when I try to insert in postgresql urls which contains '%C3' is inserted as '%%C3': For example:

https://www.amazon.es/Desesperaci%C3%B3n-BEST-SELLER-Stephen-King/dp/8497595890 is inserted as https://www.amazon.es/Desesperaci%%C3%%B3n-BEST-SELLER-Stephen-King/dp/8497595890

If the same statement is executed in sqlite3 DB there is no added '%' symbol, so the problem is when I use it in postgreSQL.

Code with CS50:

url = 'https://www.amazon.es/Desesperaci%C3%B3n-BEST-SELLER-Stephen-King/dp/8497595890'
db = cs50.SQL("postgresql://myDDBB")
db.execute("INSERT INTO urls (url, url2) VALUES (?,?)",url,'cs50')

Now if I execute it using psycopg2 package, the url is inserted with no added '%' symbol (works ok as with a sqlite3 DB).

engine = psycopg2.connect("postgres://myDDBB")
cur = engine.cursor()
cur.execute("INSERT INTO urls (url, url2) VALUES (%s,%s)", (url,"pysco2"))
engine.commit()

As seen in the DB: image

Am I missing something?

DLAcoding avatar Dec 05 '22 19:12 DLAcoding