Android SQLite
Sample CRUD Application
DataHelper.java
package com.lara.db;
import java.io.Serializable;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import com.lara.book.Book;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;
public class DataHelper implements Serializable{
private static final String DB_NAME="lara_books";
private static final int DB_VERSION=1;
private Context context;
private SQLiteDatabase db;
public DataHelper(Context context)
{
context = context;
OpenHelper openHelper = new OpenHelper(context);
db = openHelper.getWritableDatabase();
}
public long addBook(Book book)
{
SQLiteStatement stmt = db.compileStatement("insert into books values(?,?,?)");
stmt.bindString(1, book.getName());
stmt.bindString(2, book.getAuther());
stmt.bindString(3, book.getPrice());
return stmt.executeInsert();
}
public long editBook(Book book)
{
SQLiteStatement stmt = db.compileStatement("update books set name=?, auther=?,
price=? where name='" + book.getName() + "'");
stmt.bindString(1, book.getName());
stmt.bindString(2, book.getAuther());
stmt.bindString(3, book.getPrice());
return stmt.executeInsert();
}
public Book getBookDetails(String bookName)
{
Book book = null;
Cursor cursor = db.query("books", new String[]{"name", "auther", "price"},
"name='" + bookName+ "'", null, null, null, null);
Lara Technologies www.laratechnology.com 080-41310124
if(cursor.moveToFirst())
{
book = new Book();
book.setName(cursor.getString(0));
book.setAuther(cursor.getString(1));
book.setPrice(cursor.getString(2));
}
if(cursor != null && !cursor.isClosed())
{
cursor.close();
}
return book;
}
public ArrayList<Book> listBooks()
{
ArrayList<Book> list = new ArrayList<Book>();
Book book;
Cursor cursor = db.query("books", new String[]{"name, auther, price"}, null, null,
null, null, null);
if(cursor.moveToFirst())
{
do
{
book = new Book();
book.setName(cursor.getString(0));
book.setAuther(cursor.getString(1));
book.setPrice(cursor.getString(2));
list.add(book);
}while(cursor.moveToNext());
}
if(cursor != null && !cursor.isClosed())
{
cursor.close();
}
return list;
}
public void deleteBook(String book)
{
db.delete("books", "name='" + book +"'", null);
}
private static class OpenHelper extends SQLiteOpenHelper
{
public OpenHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE books (name VARCHAR NOT NULL ,
auther VARCHAR, price double)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w("Example", "Upgrading database, this will drop tables and recreate");
Lara Technologies www.laratechnology.com 080-41310124
db.execSQL("DROP TABLE IF EXISTS " + "books");
onCreate(db);
}
}
Book.java
package com.lara.book;
import java.io.Serializable;
public class Book implements Serializable{
private String name;
private String auther;
private String price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuther() {
return auther;
}
public void setAuther(String auther) {
this.auther = auther;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView android:id="@+id/home_scroll" android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<TableLayout android:id="@+id/home_tb" android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow android:layout_width="fill_parent" android:gravity="center">
<TextView android:text="Book Management"
android:layout_width="200px" android:textSize="20px"
android:layout_height="30px"/>
</TableRow>
<TableRow android:layout_width="fill_parent" android:gravity="center">
<Button android:id="@+id/add" android:text="Add Book"
android:layout_width="120px"/>
</TableRow>
<TableRow android:layout_width="fill_parent" android:gravity="center">
<Button android:id="@+id/list" android:text="List Books"
android:layout_width="120px"/>
</TableRow>
</TableLayout>
Lara Technologies www.laratechnology.com 080-41310124
</ScrollView>
AddBook.java
package com.lara.book;
import com.lara.db.DataHelper;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class AddBook extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_book);
final DataHelper dh = new DataHelper(this);
final EditText name = (EditText)findViewById(R.id.book_name);
final EditText auther = (EditText)findViewById(R.id.book_auther);
final EditText price = (EditText)findViewById(R.id.book_price);
Button cancel = (Button)findViewById(R.id.cancel_add_book);
Button save = (Button)findViewById(R.id.add_add_book);
final Intent homeIntent = new Intent(this, Home.class);
final Intent addIntent = new Intent(this, AddBook.class);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Book book = new Book();
book.setName(name.getText().toString());
book.setAuther(auther.getText().toString());
book.setPrice(price.getText().toString());
dh.addBook(book);
startActivity(homeIntent);
}
});
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
startActivity(homeIntent);
}
});
}
Lara Technologies www.laratechnology.com 080-41310124
}
add_book.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView android:id="@+id/home_scroll" android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<TableLayout android:id="@+id/home_tb" android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow android:layout_width="fill_parent" android:gravity="center">
<TextView android:text="Add Book" android:layout_width="200px"
android:textSize="20px" android:layout_height="30px"/>
</TableRow>
<TableRow android:layout_width="fill_parent" android:gravity="center">
<EditText android:id="@+id/book_name" android:hint="Name.."
android:layout_width="120px"/>
</TableRow>
<TableRow android:layout_width="fill_parent" android:gravity="center">
<EditText android:id="@+id/book_auther" android:hint="Auther.."
android:layout_width="120px"/>
</TableRow>
<TableRow android:layout_width="fill_parent" android:gravity="center">
<EditText android:id="@+id/book_price" android:hint="Price.."
android:layout_width="120px"/>
</TableRow>
<TableRow android:layout_width="fill_parent" android:gravity="center">
<Button android:id="@+id/cancel_add_book" android:text="Cancel"
android:layout_width="120px"/>
<Button android:id="@+id/add_add_book" android:text="Add"
android:layout_width="120px"/>
</TableRow>
</TableLayout>
</ScrollView>
ListBooks.java
package com.lara.book;
import java.util.ArrayList;
import com.lara.db.DataHelper;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
Lara Technologies www.laratechnology.com 080-41310124
public class ListBooks extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_books);
final DataHelper dh = new DataHelper(this);
ArrayList<Book> books = dh.listBooks();
String all[] = new String[books.size()];
for(int i=0; i<books.size(); i++)
{
all[i] = books.get(i).getName();
}
final ListView lv = (ListView)findViewById(R.id.allbooks);
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, all);
lv.setAdapter(adapter);
final Intent bookDetailsIntent = new Intent(this, BookDetails.class);
Button ok = (Button)findViewById(R.id.list_ok_books);
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
finish();
}
});
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
String bookName = lv.getItemAtPosition(position).toString();
Log.d("bookName-->>>>>", bookName);
bookDetailsIntent.putExtra("bookName", bookName);
startActivity(bookDetailsIntent);
}
});
}
}
list_books.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView android:id="@+id/home_scroll" android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
Lara Technologies www.laratechnology.com 080-41310124
<TableLayout android:id="@+id/home_tb" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow android:layout_width="fill_parent">
<TextView android:layout_width="120px" android:text="Books"
android:layout_height="30px"
android:id="@+id/listAcctslabel_tv"></TextView>
</TableRow>
<ListView android:id="@+id/allbooks" android:layout_width="fill_parent"
android:layout_height="200px">
</ListView>
<TableRow android:layout_width="fill_parent">
<Button android:id="@+id/list_ok_books" android:text="ok"
android:layout_width="120px"/>
</TableRow>
</TableLayout>
</ScrollView>
BookDetails.java
package com.lara.book;
import com.lara.db.DataHelper;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class BookDetails extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.book_details);
final DataHelper dh = new DataHelper(this);
Intent intent = getIntent();
String bookName = (String) intent.getSerializableExtra("bookName");
final Book book = dh.getBookDetails(bookName);
TextView name = (TextView)findViewById(R.id.book_name_display);
TextView auther = (TextView)findViewById(R.id.book_auther_display);
TextView price = (TextView)findViewById(R.id.book_price_display);
name.setText(book.getName());
auther.setText(book.getAuther());
price.setText(book.getPrice());
Button ok = (Button)findViewById(R.id.ok_book_details);
Lara Technologies www.laratechnology.com 080-41310124
Button edit = (Button)findViewById(R.id.edit_book_details);
Button delete = (Button)findViewById(R.id.delete_book_details);
final Intent editIntent = new Intent(this, EditBook.class);
final Intent homeIntent = new Intent(this, Home.class);
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
edit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editIntent.putExtra("book", book);
startActivity(editIntent);
}
});
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dh.deleteBook(book.getName());
startActivity(homeIntent);
}
});
}
}
book_details.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView android:id="@+id/home_scroll" android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<TableLayout android:id="@+id/home_tb" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow android:layout_width="fill_parent" android:gravity="center">
<TextView android:layout_width="120px" android:text="BookDetails"
android:layout_height="30px"
android:id="@+id/listAcctslabel_tv"></TextView>
</TableRow>
<TableRow android:layout_width="fill_parent">
<TextView android:layout_width="120px" android:text="Name"
android:layout_height="30px"></TextView>
<TextView android:layout_width="120px" android:text=""
android:layout_height="30px"
android:id="@+id/book_name_display"></TextView>
</TableRow>
<TableRow android:layout_width="fill_parent">
Lara Technologies www.laratechnology.com 080-41310124
<TextView android:layout_width="120px" android:text="Auther"
android:layout_height="30px"></TextView>
<TextView android:layout_width="120px" android:text=""
android:layout_height="30px"
android:id="@+id/book_auther_display"></TextView>
</TableRow>
<TableRow android:layout_width="fill_parent">
<TextView android:layout_width="120px" android:text="price"
android:layout_height="30px"></TextView>
<TextView android:layout_width="120px" android:text=""
android:layout_height="30px"
android:id="@+id/book_price_display"></TextView>
</TableRow>
<TableRow android:layout_width="fill_parent" android:gravity="center">
<Button android:id="@+id/ok_book_details" android:text="ok"
android:layout_width="120px"/>
</TableRow>
<TableRow android:layout_width="fill_parent" android:gravity="center">
<Button android:id="@+id/edit_book_details" android:text="edit"
android:layout_width="120px"/>
<Button android:id="@+id/delete_book_details" android:text="delete"
android:layout_width="120px"/>
</TableRow>
</TableLayout>
</ScrollView>
EditBook.java
package com.lara.book;
import com.lara.db.DataHelper;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class EditBook extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_book);
final DataHelper dh = new DataHelper(this);
Intent intent = getIntent();
Book book = (Book)intent.getSerializableExtra("book");
final EditText name = (EditText)findViewById(R.id.book_name_edit);
final EditText auther = (EditText)findViewById(R.id.book_auther_edit);
final EditText price = (EditText)findViewById(R.id.book_price_edit);
Lara Technologies www.laratechnology.com 080-41310124
name.setText(book.getName());
auther.setText(book.getAuther());
price.setText(book.getPrice());
Button cancel = (Button)findViewById(R.id.cancel_edit_book);
Button save = (Button)findViewById(R.id.save_edit_book);
final Intent addIntent = new Intent(this, EditBook.class);
final Intent homeIntent = new Intent(this, Home.class);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Book book = new Book();
book.setName(name.getText().toString());
book.setAuther(auther.getText().toString());
book.setPrice(price.getText().toString());
dh.editBook(book);
startActivity(homeIntent);
}
});
}
}
edit_book.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView android:id="@+id/home_scroll" android:layout_width="fill_parent"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<TableLayout android:id="@+id/home_tb" android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow android:layout_width="fill_parent" android:gravity="center">
<TextView android:text="Edit Book" android:layout_width="200px"
android:textSize="20px" android:layout_height="30px"/>
</TableRow>
<TableRow android:layout_width="fill_parent" android:gravity="center">
<EditText android:id="@+id/book_name_edit" android:hint="Name.."
android:layout_width="120px"/>
</TableRow>
<TableRow android:layout_width="fill_parent" android:gravity="center">
<EditText android:id="@+id/book_auther_edit" android:hint="Auther.."
android:layout_width="120px"/>
</TableRow>
<TableRow android:layout_width="fill_parent" android:gravity="center">
<EditText android:id="@+id/book_price_edit" android:hint="Price.."
android:layout_width="120px"/>
</TableRow>
<TableRow android:layout_width="fill_parent" android:gravity="center">
<Button android:id="@+id/cancel_edit_book" android:text="Cancel"
android:layout_width="120px"/>
<Button android:id="@+id/save_edit_book" android:text="Save Changes"
android:layout_width="120px"/>
</TableRow>
</TableLayout>
</ScrollView>
Lara Technologies www.laratechnology.com 080-41310124