• Index
  •  » Talend Open Studio for Data Integration » Usage, Operation
  •  » Inserting quotation marks in MySQL database.

#1 2012-07-12 18:17:25

sdel
Member
Registered: 2012-05-25
Posts: 86

Inserting quotation marks in MySQL database.

I have a tJavaRow connected to  a tMysqlRow

I need to insert a string that contain a ‘  for esample L’some name. This needs to be escaped in the insert statment

INSERT INTO books (title) VALUES (‘L\’some name’);

However if I hard code this statement in the tMysqlRow I need to add a second \ as tMysqlRow removes the first

INSERT INTO books (title) VALUES (‘L\\’some name’);

This is ok but in my job I am using the tJavaRow to add the \\ before the ‘ because tJavaRow also removes the \ characters I have added two more \\

output_row.title = input_row.title.replace("'", "\\\\'");

This leaves \\ before the ‘ when its is sent to the tMysqlRow but this fails

Please let me know if you know why.


Uploaded Images

Last edited by sdel (2012-07-12 18:22:25)

Offline

#2 2012-07-13 03:17:06

pedro
Member
Registered: 2011-11-17
Posts: 3682

Re: Inserting quotation marks in MySQL database.

Hi

Why do you add a second \ in tJavaRow?
The following string is ok.
"INSERT INTO books (title) VALUES ('L\'some name');"

Besides, I notice that your quotation is different from the normal quotation ' '.
Yours is ``.

Regards,
Pedro


Only Paranoid Survive.

Offline

#3 2012-07-13 10:44:24

sdel
Member
Registered: 2012-05-25
Posts: 86

Re: Inserting quotation marks in MySQL database.

I am not sure why the quotes display differently but they are the correct quotes.

"INSERT INTO books (title) VALUES ('L\'some name');" if used in a tMysqlRow fails I have tried it. "INSERT INTO books (title) VALUES ('L\\'some name');" with two \ works.

I only have one tJavaRow I need to use this because I do not know where in the string the ' will be.

If I add two \ using the code below and send the output to a tlog I can see that both are stripped off.

output_row.title = input_row.title.replace("'", "\\'");

If I add four then two get through but they do not get inserted to the db.

If you know why this is please let me know.

Offline

#4 2012-07-13 11:50:24

pedro
Member
Registered: 2011-11-17
Posts: 3682

Re: Inserting quotation marks in MySQL database.

Hi

Code:

if used in a tMysqlRow fails I have tried it

Do you get any error? Or what result do you get?

Regards,
Pedro


Only Paranoid Survive.

Offline

#5 2012-07-13 12:03:29

sdel
Member
Registered: 2012-05-25
Posts: 86

Re: Inserting quotation marks in MySQL database.

I get "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'some name'" and it does not insert.
Thanks

Offline

#6 2012-07-13 12:43:24

pedro
Member
Registered: 2011-11-17
Posts: 3682

Re: Inserting quotation marks in MySQL database.

Hi

Here is a workaround.
First add a context variable in your job(e.g. context.new1).
Then code in tJavaRow as below.

Code:

context.new1 = "INSERT INTO books (title) VALUES ('"+input_row.title.replace("'", "\\'")+"');";

Then type context.new1 in the query field of tMysqlRow.

I test it and it works fine.

Regards,
Pedro


Only Paranoid Survive.

Offline

#7 2012-07-13 13:12:08

sdel
Member
Registered: 2012-05-25
Posts: 86

Re: Inserting quotation marks in MySQL database.

Thanks for your help.

Offline

#8 2012-07-16 07:31:23

pedro
Member
Registered: 2011-11-17
Posts: 3682

Re: Inserting quotation marks in MySQL database.

Hi

Glad to help you.
Feel free to ask any question here.

Regards,
Pedro


Only Paranoid Survive.

Offline

  • Index
  •  » Talend Open Studio for Data Integration » Usage, Operation
  •  » Inserting quotation marks in MySQL database.

Board footer

Powered by FluxBB