I've made a Java code that reads a text document and returns me a list of sql commands (insert into
) that I copy and paste on pgAdmin III so it can insert the data read in its database.
As you know, the insert into
comand uses simple quotes to wrap string values, as it follows:
insert into table (description) values ('text from the .txt');
The thing is, some descriptions come with simple quotes in its text, and so, it makes pgAdmin understand those as the end of the string:
insert into table (description) values ('this is a description where ' this simple quote messes up the command');
I also have to do the same with Java expressions, like:
if(Currentline.charAt(i) == ''' ){ ... }
So I can fix the entire problem.
I think, probably, there is a simples way to solve that. Does anybody knows how to do it? Basically, how to make the pgAdmin incorporate this character to the string?
What you are looking for is how to escape special characters. Therefore, you have to escape '
with '
which means that you would end with ''
, being your insert like this:
insert into table (description)
values ('this is a description where '' this simple quote messes up the command');
Edit: since you updated your question with the java code. For java you also need to escape your special character, in this case with \
. Your code would be:
if(Currentline.charAt(i) == '\'' ){ ... }