OrmLite - Lightweight Java ORM Supports Android and SQLite

ORM Lite

Object Relational Mapping Lite (ORM Lite) provides some lightweight functionality for persisting Java objects to SQL databases while avoiding the complexity and overhead of more standard ORM packages. It supports a number of SQL databases using JDBC and also supports Sqlite with native calls to Android OS database APIs. Documentation about how to configure ORMLite for Android specifically is available in the manual.

Please contact me with any feedback or questions about Android support. See the home page for more details.

ORMLite provides the following features:

Use Annotations to Mark Classes to be Persisted

To use the package you add the @DatabaseTable annotation to the top of each class and a @DatabaseField annotation to each of the fields in the class that are to be persisted to the database. For example:

@DatabaseTable(tableName = "accounts") public class Account { @DatabaseField(id = true) private String name; @DatabaseField(canBeNull = false) private String password; ... Account() { // all persisted classes must define a no-arg constructor with at least package visibility } ... }

Classes can also be configured with javax.persistence annotations, Java calls, or Spring wiring. For more details, see the online documentation.

Sample Code Example

The ORMLite Android code includes base activity, service, and tab classes to help create and manage your database connections and DAOs. The following is a quick code example to give you a taste on how to use the package.

// you get the SQLiteOpenHelper from your Android Activity ConnectionSource connectionSource = new AndroidConnectionSource(sqliteOpenHelper); // instantiate the DAO to handle Account with String id Dao<Account,String> accountDao = BaseDaoImpl.createDao(connectionSource, Account.class); // if you need to create the 'accounts' table make this call TableUtils.createTable(connectionSource, Account.class); // create an instance of Account String name = "Jim Smith"; Account account = new Account(name, "_secret"); // persist the account object to the database // it should return 1 for the 1 row inserted if (accountDao.create(account) != 1) { throw new Exception("Failure adding account"); } // retrieve the account Account account2 = accountDao.queryForId(name); // show its password System.out.println("Account: " + account2.getPassword()); // close the connection source connectionSource.close();

[an error occurred while processing this directive] [an error occurred while processing this directive]