-1

I have a string like these "Marko's place" "boulevard "Arequipa"" strings that containing single or double quotes, in Java using regular expressions how get that the previous strings get like this "Marko\s place" "boulevard \"Arequipa\"" I am no have experience with regex, thank for any answer

OK, I am reading records from a mysql table and constructing the insert sql to pass this information to a sqlite table

    Statement qryMySQL= cnMySql.createStatement();
        ResultSet rs = qryMySQL.executeQuery("select * from tblclientes ");

        Statement qrySQLite = cnSqLite.createStatement();
        qrySQLite.addBatch("begin transaction");
        qrySQLite.addBatch("CREATE TABLE 'tblclientes' ('_id' INTEGER,'Nombres' varchar(25) DEFAULT NULL,'Apellidos' varchar(25) DEFAULT NULL,'RazSocial' varchar(20) DEFAULT NULL,'Direccion' varchar(50) DEFAULT NULL,'Ciudad' varchar(15) DEFAULT 'Arequipa','Fono' varchar(12) DEFAULT NULL,'Fax' varchar(12) DEFAULT NULL,'Email' varchar(35) DEFAULT NULL,'Ruc' varchar(12) DEFAULT NULL,'latitud' decimal(20,14) DEFAULT NULL,'longitud' decimal(20,14) DEFAULT NULL,'ruta' varchar(10) DEFAULT NULL,'sincro' CHAR(10),'copiar' BOOL DEFAULT 1)");

        while (rs.next()) 
        { 
            //System.out.println (rs.getInt(1) + " " + rs.getString (2) );
            sql = "INSERT INTO tblclientes(_id,nombres,apellidos) " +
                    "VALUES("+rs.getInt("id")+", \""+rs.getString("nombres")+"\",\""+rs.getString("apellidos")+"\")";
            qrySQLite.addBatch(sql);
           // System.out.println (sql);
        }
        qrySQLite.addBatch("end transaction");
        qrySQLite.executeBatch();

but some fields in the mysql table have the characters " and ' that causes error in sql insert sentence then I Need this

<Marko's place> ===> <Marko\'s place>
<boulevard "Arequipa"> ====> <boulevard \"Arequipa">

so the result must be add the \ before the " or '

Delimitry
  • 2,987
  • 4
  • 30
  • 39
David
  • 95
  • 1
  • 8
  • 1
    Please describe exactly what you need (use a few examples). The example you've provided does not clearly define what you're looking for. Will you be processing one string at a time or multiple strings? It looks like you want to preserve opening and closing double quotes, but it's not clear how they should be defined. Is your input a _single_ string or multiple strings? Are they delimited by newlines? – jahroy Dec 06 '13 at 02:21
  • Thank you for clarifying. Here are a few questions and answers about escaping SQL strings: [one](http://stackoverflow.com/q/1812891/778118), [two](http://stackoverflow.com/q/7006586/778118), [three](http://stackoverflow.com/q/10522257/778118). – jahroy Dec 06 '13 at 02:58
  • 1
    The [escapeSql()](http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringEscapeUtils.html#escapeSql(java.lang.String)) and/or [escapeJava()](http://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringEscapeUtils.html#escapeJava(java.lang.String)) methods from Apache's StringEscapeUtils look like what they do exactly what you need. – jahroy Dec 06 '13 at 03:02
  • Note that quotes in SQL strings are usually escaped by another quote character (not a backslash). For example, you would typically use the following to escape _Joe's Diner_: `Joe''s Diner` – jahroy Dec 06 '13 at 03:06
  • the The escapeSql() method is exacty that I need thanks – David Dec 06 '13 at 03:20

1 Answers1

1

I would probably use the Apache Commons StringEscapeUtils it catches a lot more corner cases then you have in your question -

StringEscapeUtils.escapeJava(str);

Otherwise, you could just use string replace like this

String str = "\"Marko's place\"";
System.out.println(str.replace("'", "\\'").replace("\"", "\\\""));
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249