Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

psycopg, double and single quotes insert

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

like image 697
Ostap Khl Avatar asked Apr 30 '15 10:04

Ostap Khl


People also ask

Can you use double quotes in PostgreSQL?

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.

How do I add a quote to a string in Oracle?

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.

How do you add quotes in PostgreSQL?

In Postgresql, we can insert a single quote using the double single quote (”) or (E'\') to declare Posix escape string syntax.

How can I replace single quotes with double quotes in Oracle?

SQL> SELECT REPLACE('STN. "A"', '"', q'(')') FROM Dual; REPLACE( -------- STN. 'A'{code} Nicoals.


1 Answers

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))
like image 62
Michał Niklas Avatar answered Sep 29 '22 10:09

Michał Niklas