Skip to content

Interacting with the Database

njkremer edited this page Apr 23, 2013 · 6 revisions

First you'll need to initialized the connection to the database. You do this with the DataConnectionManager.init(String) or DataConnectionManager.init(String, String) method.

Once the connection is initialized you will use the static methods off of SqlStatement to interact with the database/objects.

Code Examples:

Retrieving a User from the database:

User u = SqlStatement.select(User.class).where("name").eq("Nick").getList().get(0);

Retreiving a list of Users whose name starts with the letter 'A':

List<User> users = SqlStatement.select(User.class).where("name").like("A%").getList();

Changing the User with the name = Nick's password:

User nick = SqlStatement.select(User.class).where("name").eq("nick").getFirst();
nick.setPassword("ABC123");
SqlStatement.update(nick).execute(); // Note this is if you define a @PrimaryKey annotation 
                             // (see 'Slightly More Advanced "Mapping"' section for more info).

Inserting a new User into the database:

User newUser = new User();
newUser.setName("Bob");
newUser.setPassword("123456");
SqlStatement.insert(newUser).execute();

Retreiving a list of all users in descending order:

List<User> users = SqlStatement.select(User.class).orderBy("name").desc().getList();

Getting just the count of all the users in the database:

int numOfUsers = SqlStatement.select(User.class).getCount();

The JavaDocs have a pretty good outline of what is possible with interactions. Note that after you start your SqlStatement a SqlExecutor is returned for function chaining. So when looking at the JavaDocs you may want to look at the SqlExecutor class.

Clone this wiki locally