I ran into problems, while trying to insert to database:
ur_psql.execute("""insert into smth(data, filedate, filedby)"""
""" values('%s', NOW(), %s)""" % (data, userid))
where data=""" "5.09,448,1418112000"; d="scan'208" """
(a string containing double and single quotes)
Any ideas how to insert such string to db ?
Thanks
In PostgreSQL, double quotes (like "a red dog") are always used to denote delimited identifiers. In this context, an identifier is the name of an object within PostgreSQL, such as a table name or a column name. Delimited identifiers are identifiers that have a specifically marked beginning and end.
If you want a single quote to appear in the middle of a string add another single quote to it. If you want a single quote to appear at the beginning or end of a string add 2 single quotes to it. If you want a single quote to appear on its own add 3 single quotes to it.
In Postgresql, we can insert a single quote using the double single quote (”) or (E'\') to declare Posix escape string syntax.
SQL> SELECT REPLACE('STN. "A"', '"', q'(')') FROM Dual; REPLACE( -------- STN. 'A'{code} Nicoals.
You can read about it at: http://initd.org/psycopg/docs/usage.html#the-problem-with-the-query-parameters
Simply do not use quotes in SQL and instead of %
string Python operator use 2nd parameter of execute()
which is data you want to pass to SQL query:
sql = "insert into smth (data, filedate, filedby) values (%s, NOW(), %s)"
ur_psql.execute(sql, (data, userid))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With