Hi Guys!,
Today we are learning about the OrmLite database in android.
OrmLite is the lightweight java ORM supports for android Sqlite Database. The full OrmLite is Object Relational Mapping (ORMLite). and it provide some light weight functionality to store and retrieve java objects. OrmLite 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 api. 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.
OrmLite simpley add Objects of java using annotations and it have powerful abstract Database Object classes also provides simple and flexible query using QueryBuilder. Auto generates SQL to create and drop database tables. And it have basic supports for database transactions.
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.
Users that are connecting to SQL databases via JDBC connections will need to download the
Here is the link to download these jar files OrmLite Jars.
Or more ablout the ORMLite you can visit the ORL Developer site Here.
So In that scenario you need to first create the Pojo class means data model. Lets start the coding.
Today we are learning about the OrmLite database in android.
OrmLite is the lightweight java ORM supports for android Sqlite Database. The full OrmLite is Object Relational Mapping (ORMLite). and it provide some light weight functionality to store and retrieve java objects. OrmLite 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 api. 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.
OrmLite simpley add Objects of java using annotations and it have powerful abstract Database Object classes also provides simple and flexible query using QueryBuilder. Auto generates SQL to create and drop database tables. And it have basic supports for database transactions.
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.
Users that are connecting to SQL databases via JDBC connections will need to download the
ormlite-jdbc-4.46.jar
and ormlite-core-4.46.jar
files. For use with Android applications, you should download the ormlite-android-4.46.jar
and ormlite-core-4.46.jar
files.Here is the link to download these jar files OrmLite Jars.
Or more ablout the ORMLite you can visit the ORL Developer site Here.
So In that scenario you need to first create the Pojo class means data model. Lets start the coding.
main.xml
row.xml
Person.java
package com.sunil.ormlite;
import com.j256.ormlite.field.DatabaseField;
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;
}
}
DatabaseHelper.java
package com.sunil.ormlite;
import java.sql.SQLException;
import java.util.List;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.RuntimeExceptionDao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
private Context context;
private static final String DATABASE_NAME = "ormliteandroid.db";
private static final int DATABASE_VERSION = 1;
private DaosimpleDao = null;
private RuntimeExceptionDaosimpleRuntimeDao = null;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
context = context;
}
public DaogetDao() throws SQLException {
if (simpleDao == null) {
simpleDao = getDao(Person.class);
}
return simpleDao;
}
public RuntimeExceptionDaogetSimpleDataDao() {
if (simpleRuntimeDao == null) {
simpleRuntimeDao = getRuntimeExceptionDao(Person.class);
}
return simpleRuntimeDao;
}
//method for list of person
public ListGetData()
{
DatabaseHelper helper = new DatabaseHelper(context);
RuntimeExceptionDaosimpleDao = helper.getSimpleDataDao();
Listlist = simpleDao.queryForAll();
return list;
}
//method for insert data
public int addData(Person person)
{
RuntimeExceptionDaodao = getSimpleDataDao();
int i = dao.create(person);
return i;
}
//method for delete all rows
public void deleteAll()
{
RuntimeExceptionDaodao = getSimpleDataDao();
Listlist = dao.queryForAll();
dao.delete(list);
}
@Override
public void close() {
super.close();
simpleRuntimeDao = null;
}
@Override
public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {
try {
Log.i(DatabaseHelper.class.getName(), "onCreate");
TableUtils.createTable(connectionSource, Person.class);
} catch (SQLException e) {
Log.e(DatabaseHelper.class.getName(), "Can't create database", e);
throw new RuntimeException(e);
}
}
@Override
public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int oldVersion, int newVersion) {
try {
Log.i(DatabaseHelper.class.getName(), "onUpgrade");
TableUtils.dropTable(connectionSource, Person.class, true);
onCreate(db, connectionSource);
} catch (SQLException e) {
Log.e(DatabaseHelper.class.getName(), "Can't drop databases", e);
throw new RuntimeException(e);
}
}
}
NameAdapter.java
package com.sunil.ormlite;
import java.util.List;
import com.sunil.ormlite.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class NameAdapter extends ArrayAdapter{
private Context mContext;
private int row;
private Listlist ;
public NameAdapter(Context context, int textViewResourceId, Listlist) {
super(context, textViewResourceId, list);
this.mContext=context;
this.row=textViewResourceId;
this.list=list;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder holder;
if (view == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(row, null);
holder = new ViewHolder();
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
if ((list == null) || ((position + 1) > list.size()))
return view; // Can't extract item
Person obj = list.get(position);
holder.name = (TextView)view.findViewById(R.id.tvname);
if(null!=holder.name&&null!=obj&&obj.getName().length()!=0){
holder.name.setText(obj.getName());
}
return view;
}
public static class ViewHolder {
public TextView name;
}
}
MainActivity.java
package com.sunil.ormlite;You can download the source code OrmLite Database in Android
import java.util.List;
import com.sunil.ormlite.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnItemClickListener {
EditText etEntry;
ListView listView;
NameAdapter adapter = null;
DatabaseHelper helper;
Listlist;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView = (ListView) findViewById(R.id.listView1);
listView.setOnItemClickListener(this);
etEntry = (EditText) findViewById(R.id.etentry);
helper = new DatabaseHelper(getApplicationContext());
setDataToAdapter();
}
public void adddata(View v) {
String strName = etEntry.getText().toString().trim();
if (TextUtils.isEmpty(strName)) {
showToast("Please Add your Name!!!");
return;
}
Person person = new Person();
person.setName(strName);
helper.addData(person);
showToast("Data Successfully Added");
setDataToAdapter();
}
private void setDataToAdapter() {
list = helper.GetData();
adapter = new NameAdapter(this, R.layout.row, list);
listView.setAdapter(adapter);
}
private void showToast(String msg) {
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}
@SuppressWarnings("deprecation")
public void deletedata(View v) {
list = helper.GetData();
if (null != list && list.size() > 0) {
AlertDialog alert = new AlertDialog.Builder(MainActivity.this)
.create();
alert.setTitle("Delete ?");
alert.setMessage("Are you sure want to delete All data from Database");
alert.setButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alert.setButton2("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
helper.deleteAll();
etEntry.setText("");
showToast("Removed All Data!!!");
setDataToAdapter();
}
});
alert.show();
} else {
showToast("No data found from the Database");
}
}
@Override
public void onItemClick(AdapterView parent, View view, int position,
long id) {
showToast(list.get(position).getName());
}
}
Update Contact :
No Wa/Telepon (puat) : 085267792168
No Wa/Telepon (fajar) : 085369237896
Email : Fajarudinsidik@gmail.com
No Wa/Telepon (puat) : 085267792168
No Wa/Telepon (fajar) : 085369237896
Email: Fajarudinsidik@gmail.com
atau Kirimkan Private messanger melalui email dengan klik tombol order dibawah ini :