This project is designed to be a very lightweight ORM for interacting with a SQLite Database. The concept behind it is more convention over configuration. Meaning that, to map an Java Object to a database table, there are very few steps needed:
That's it!
Example:
public class User {
private String name;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
Would map to a database table that looks like this, with the name of user:
column name | type |
---|---|
name | text |
password | text |
For slightly more "advanced" usage, you can specify two different types of Annotations on your POJO.
Example:
public class User {
@AutoIncrement
@PrimaryKey
private long id;
private String name;
private String password;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
Which would map to a table named user that looks like:
column name | type | PK? |
---|---|---|
id | auto incremented int | yes |
name | text | no |
password | text | no |
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");
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.
For more information on how to use this library please see the GitHub Wiki
This software is licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.