I have a table test(id,name)
.
I need to insert values like: user's log
, 'my user'
, customer's
.
insert into test values (1,'user's log');
insert into test values (2,''my users'');
insert into test values (3,'customer's');
I am getting an error if I run any of the above statements.
If there is any method to do this correctly please share. I don't want any prepared statements.
Is it possible using sql escaping mechanism?
According to PostgreSQL documentation (4.1.2.1. String Constants):
See also the standard_conforming_strings parameter, which controls whether escaping with backslashes works.
If you need to get the work done inside Pg:
to_json(value)
https://www.postgresql.org/docs/9.3/static/functions-json.html#FUNCTIONS-JSON-TABLE
In postgresql if you want to insert values with
'
in it then for this you have to give extra'
you can use the postrgesql chr(int) function:
Escaping single quotes
'
by doubling them up ->''
is the standard way and works of course.In old versions or if you still run with
standard_conforming_strings = off
or, generally, if you prepend your string withE
to declare Posix escape string syntax, you can also escape with the backslash\
:But that's generally not preferable.
If you have to deal with many single quotes or multiple layers of escaping, you can avoid quoting hell in PostgreSQL with dollar-quoted strings:
To further avoid confusion among dollar-quotes, add a unique token to each pair:
Which can be nested any number of levels:
Pay attention if the
$
character should have special meaning in your client software. You may have to escape it in addition. This is not the case with standard PostgreSQL clients like psql or pgAdmin.That is all very useful for writing plpgsql functions or ad-hoc SQL commands. It cannot alleviate the need to use prepared statements or some other method to safeguard against SQL injection in your application when user input is possible, though. @Craig's answer has more on that. More details:
This is so many worlds of bad, because your question implies that you probably have gaping SQL injection holes in your application.
You should be using parameterized statements. For Java, use
PreparedStatement
with placeholders. You say you don't want to use parameterised statements, but you don't explain why, and frankly it has to be a very good reason not to use them because they're the simplest, safest way to fix the problem you are trying to solve.See Preventing SQL Injection in Java. Don't be Bobby's next victim.
There is no public function in PgJDBC for string quoting and escaping. That's partly because it might make it seem like a good idea.
There are built-in quoting functions
quote_literal
andquote_ident
in PostgreSQL, but they are forPL/PgSQL
functions that useEXECUTE
. These daysquote_literal
is mostly obsoleted byEXECUTE ... USING
, which is the parameterised version, because it's safer and easier. You cannot use them for the purpose you explain here, because they're server-side functions.Imagine what happens if you get the value
');DROP SCHEMA public;--
from a malicious user. You'd produce:which breaks down to two statements and a comment that gets ignored:
Whoops, there goes your database.