Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to escape " ' " in sql

Hi I am searching for a name combination in database. I am passing the combination as follows

"firstName='" + firstName + "'", "middleName='" + middleName + "'", "lastName='" + lastName + "'"

This works fine. But the problem comes where there are some " ' " in names how can i rectify it? eg: Johns' or Jerry's etc causes exception

like image 928
Jaison Avatar asked Dec 04 '22 16:12

Jaison


2 Answers

use preparedStatement it is easy for you

ps.executeUpdate("INSERT INTO tb_name values(?,?) WHERE id=?");
ps.setString(1,firstName);
ps.setString(2,middleName);
ps.setInt(3,id);
ps.execute();
like image 164
chaitu Avatar answered Dec 08 '22 06:12

chaitu


At least for MySQL, you have to put another ' before:

INSERT INTO table (column) VALUES ('this isn''t it');
like image 30
sp00m Avatar answered Dec 08 '22 04:12

sp00m