Android ORMLite Database Tutorial



















ORMLite is lightweight Java ORM supports for android Sqlite Database.The full form of ORMLite is Object Relational Mapping Lite(ORMLite).and it provides some light weight functionality to store and retrieve Java Objects.And it avoiding the complexity and more standard Object Relational Mapping.


ORMLite supports more than one database using JDBC and also sqlite with native calls to android database APIs..


ORMLite simply add objects of java using annotations.and its have powerful abstract Database Access Object classes.also provides simple and  flexible query using QueryBuilder.Auto generates SQL to create and drop database tables.and its have basic supports for database transactions.



ORMLite Supports MySQL, Postgres, Microsoft SQL Server, H2, Derby, HSQLDB, and Sqlite and can be extended to additional databases relatively easily.
Provisional support for DB2, Oracle, ODBC, and Netezza.


        
        @DatabaseField(generatedId = true)
private int id;
        @DatabaseField
        private String name;
        @DatabaseField 
        private String adress;
Suppose, your class is Person
public class Person {

        @DatabaseField(generatedId = true)
        private int id;
        @DatabaseField
        private String name;
 public int getId() {
  return id;
 }
 public void setId(int id) {
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String firstname) {
  this.name = firstname;
 }
}
First of All Create object of DatabaseHelper Class.
DatabaseHelper helper = new DatabaseHelper(getApplicationContext());
 Now using helper object you can add java object in ORMLite Database. Person person = new Person(); person.setName(strName);
  helper.addData(person); // Add Object in ORMLite Database

Now how to Retrieve Data From ORMLite Database

  List<Person> list = helper.GetData();

Delete Data From ORMLite Database.

  helper.deleteAll();

DownLoad Full Source Code From Here

          DownLoad Source Code

4 comments:

  1. For the first time it works fine but from the second time it is crashing....
    05-18 12:07:45.501: E/AndroidRuntime(274): FATAL EXCEPTION: main
    05-18 12:07:45.501: E/AndroidRuntime(274): java.lang.NoClassDefFoundError: com.samir.ormliste.DatabaseHelper
    05-18 12:07:45.501: E/AndroidRuntime(274): at com.samir.ormliste.MainActivity.onCreate(MainActivity.java:34)
    05-18 12:07:45.501: E/AndroidRuntime(274): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    05-18 12:07:45.501: E/AndroidRuntime(274): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
    05-18 12:07:45.501: E/AndroidRuntime(274): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
    05-18 12:07:45.501: E/AndroidRuntime(274): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
    05-18 12:07:45.501: E/AndroidRuntime(274): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
    05-18 12:07:45.501: E/AndroidRuntime(274): at android.os.Handler.dispatchMessage(Handler.java:99)
    05-18 12:07:45.501: E/AndroidRuntime(274): at android.os.Looper.loop(Looper.java:123)
    05-18 12:07:45.501: E/AndroidRuntime(274): at android.app.ActivityThread.main(ActivityThread.java:4627)
    05-18 12:07:45.501: E/AndroidRuntime(274): at java.lang.reflect.Method.invokeNative(Native Method)
    05-18 12:07:45.501: E/AndroidRuntime(274): at java.lang.reflect.Method.invoke(Method.java:521)
    05-18 12:07:45.501: E/AndroidRuntime(274): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    05-18 12:07:45.501: E/AndroidRuntime(274): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    05-18 12:07:45.501: E/AndroidRuntime(274): at dalvik.system.NativeStart.main(Native Method)

    ReplyDelete
    Replies
    1. Have you found the solution? (I have the same problem, although class is in project)

      Delete
    2. Solution found:

      Right Click on project -> Android Tools -> Add Support Library, then let it update / install..Clean the project and then run!

      Delete
    3. Sathyabrata Panda

      did u recently updated your eclipse android plugin ?If so then , Right click on properties, then click on java build path and then on order and export just add the library and run .Hope this will work

      Delete

Android Testing App