Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JDO for Google App Engine: escaping quotes

How do I escape parameters of queries in JDO (Google App Engine)?

For example, how do I make the next snippet safe, if the variable name may contain unsafe chars as single quotes (')

PersistenceManager pm = ...;
String query = "select from Person where name='"+name+"'";
List<Shortened> shortened = (List<Shortened>) pm.newQuery(query).execute();
like image 378
flybywire Avatar asked Sep 30 '09 09:09

flybywire


1 Answers

Use query parameters instead, it's a much safer than including the values in the query itself. Here is an example from the GAE documentation:

Query query = pm.newQuery("select from Employee " +
                          "where lastName == lastNameParam " +
                          "order by hireDate desc " +
                          "parameters String lastNameParam");

List<Employee> results = (List<Employee>) query.execute("Smith");
like image 109
Todd Owen Avatar answered Sep 23 '22 10:09

Todd Owen