Avadhani Y
Avadhani Y

Reputation: 7636

How to search with the special characters in android

I have one App working with the dataabase in which the organisation column having the records with the special characters(single quote). I am getting force close error while searching with the special character in the text field.

The code is below:

    String name = idValues.getStringExtra("ORGNAME");
   String sql ="select orgname from table1 where name like '%"+ name +"%'  Order by org.name";

Please help me with the samplecode/links to search with the name containing special character.

Thanks in advance.

Upvotes: 2

Views: 1407

Answers (2)

Kannan Suresh
Kannan Suresh

Reputation: 4580

String name = idValues.getStringExtra("ORGNAME");    

if (name.contains("'")) {
   // Should replace all the charecters
   name = name.replaceAll("'", "''");
}

String sql ="select orgname from table1 where name like '%"+ name +"%'  Order by org.name"; 

Replace the single quote(') in the String as shown. Please try this.

Upvotes: 3

Sandeep Pathak
Sandeep Pathak

Reputation: 10747

Try Using PreparedStatement like this :

PreparedStatement pstmt = null;
String sql ="select orgname from table1 where name like ? Order by orgname";
pstmt.setString(1,"%"+name+"%");
ResultSet rs = pstmt.executeQuery();

No Error checks have been done .

Upvotes: 1

Related Questions