Link to home
Create AccountLog in
Avatar of HycelTaylor
HycelTaylor

asked on

How to insert text with double quotes and semi colins in an insert statement

I have to use a created sql statement to insert into a table.  One of the colums needs to take a short sentance.  A sentance may have double quotes (") and semi colins (,).

Example: This, is a "Test"

My code is the following:

 sql = new StringBuilder("insert into t_content (activation_datetime, text, ) values (");
 sql.append(dateFormat.format(content.getActivationDateTime())).append(COMMA);
 String text = content.getText();

 // do something to the text here

 sql.append(text);
 sql.append(")");
 insertStatment.addBatch(sql.toString());

I also need to make sure that once the text is save to the database it's back into it original format.
I need a solution to this problem urgently.

Thanks for any help in this matter.
Avatar of Raynard7
Raynard7

Hi - you need to replace the offending characters with escape character \

you should only need to replace the following characters:-
carraige return -> \r
line feed -> \n
tab -> \t
' -> \'
" -> \"
Avatar of HycelTaylor

ASKER


The insert statement would look like the following:

insert into t_content(1000,This, is a \"Test\");

I think that I would still need to place something around the sentance so that the comma would not be mistaken for a column separator.

What should I place around the sentance?
ASKER CERTIFIED SOLUTION
Avatar of Raynard7
Raynard7

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer