Bind VariablesΒΆ

Dari supports bind variables in query strings using ? for placeholders. In the following code snippet, the userName variable is substituted for the placeholder in the query.

 String userName = "John Smith";
 User user = Query.from(User.class)
                 .where("name = ?", userName).first();

Placeholders can be basic types like String or Integer, but they can also be lists or other Java objects. In the following code snippet, the query searches for all instances of User with a name value that matches any of the names in the list.

 List<String> names = new ArrayList<String>();
 names.add("John Smith");
 names.add("Jane Doe");
 List<User> users = Query.from(User.class)
                         .where("name = ?", names).selectAll();

In the next example, the query filters on a Date object, returning all Activity instances of the specified date.

String startDateString = "02/29/2017";
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
Date date = df.parse(startDateString);
List<Activity> activities = Query.from(Activity.class)
                            .where("activityDate = ?", date).selectAll();