0% found this document useful (0 votes)
9 views10 pages

Android SQLite CRUD Book App

This document describes an Android application that uses SQLite to perform CRUD (create, read, update, delete) operations on a book database. It includes classes for a DataHelper to manage the database, a Book model, and activities for adding, listing, and viewing books. The main activity contains buttons to trigger adding and listing books. The add activity contains fields to enter book details and buttons to save or cancel the addition.

Uploaded by

Gayatri Joshi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views10 pages

Android SQLite CRUD Book App

This document describes an Android application that uses SQLite to perform CRUD (create, read, update, delete) operations on a book database. It includes classes for a DataHelper to manage the database, a Book model, and activities for adding, listing, and viewing books. The main activity contains buttons to trigger adding and listing books. The add activity contains fields to enter book details and buttons to save or cancel the addition.

Uploaded by

Gayatri Joshi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

Android SQLite

Sample CRUD Application


[Link]
package [Link];

import [Link];
import [Link];
import [Link];
import [Link];

import [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

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 = [Link]();
}

public long addBook(Book book)


{
SQLiteStatement stmt = [Link]("insert into books values(?,?,?)");
[Link](1, [Link]());
[Link](2, [Link]());
[Link](3, [Link]());
return [Link]();
}
public long editBook(Book book)
{
SQLiteStatement stmt = [Link]("update books set name=?, auther=?,
price=? where name='" + [Link]() + "'");
[Link](1, [Link]());
[Link](2, [Link]());
[Link](3, [Link]());
return [Link]();
}

public Book getBookDetails(String bookName)


{
Book book = null;
Cursor cursor = [Link]("books", new String[]{"name", "auther", "price"},
"name='" + bookName+ "'", null, null, null, null);

Lara Technologies [Link] 080-41310124


if([Link]())
{
book = new Book();
[Link]([Link](0));
[Link]([Link](1));
[Link]([Link](2));
}
if(cursor != null && ![Link]())
{
[Link]();
}
return book;
}

public ArrayList<Book> listBooks()


{
ArrayList<Book> list = new ArrayList<Book>();
Book book;
Cursor cursor = [Link]("books", new String[]{"name, auther, price"}, null, null,
null, null, null);
if([Link]())
{
do
{
book = new Book();
[Link]([Link](0));
[Link]([Link](1));
[Link]([Link](2));
[Link](book);
}while([Link]());
}
if(cursor != null && ![Link]())
{
[Link]();
}
return list;
}

public void deleteBook(String book)


{
[Link]("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) {
[Link]("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 [Link] 080-41310124


[Link]("DROP TABLE IF EXISTS " + "books");
onCreate(db);
}
}

[Link]
package [Link];

import [Link];

public class Book implements Serializable{


private String name;
private String auther;
private String price;
public String getName() {
return name;
}
public void setName(String name) {
[Link] = name;
}
public String getAuther() {
return auther;
}
public void setAuther(String auther) {
[Link] = auther;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
[Link] = price;
}
}

[Link]
<?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="[Link]
<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 [Link] 080-41310124


</ScrollView>

[Link]
package [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class AddBook extends Activity {


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].add_book);

final DataHelper dh = new DataHelper(this);

final EditText name = (EditText)findViewById([Link].book_name);


final EditText auther = (EditText)findViewById([Link].book_auther);
final EditText price = (EditText)findViewById([Link].book_price);

Button cancel = (Button)findViewById([Link].cancel_add_book);


Button save = (Button)findViewById([Link].add_add_book);

final Intent homeIntent = new Intent(this, [Link]);


final Intent addIntent = new Intent(this, [Link]);

[Link](new [Link]() {
@Override
public void onClick(View arg0) {
Book book = new Book();
[Link]([Link]().toString());
[Link]([Link]().toString());
[Link]([Link]().toString());
[Link](book);
startActivity(homeIntent);
}
});
[Link](new [Link]() {
@Override
public void onClick(View arg0) {
startActivity(homeIntent);
}
});
}

Lara Technologies [Link] 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="[Link]
<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>

[Link]
package [Link];

import [Link];

import [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

Lara Technologies [Link] 080-41310124


public class ListBooks extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].list_books);

final DataHelper dh = new DataHelper(this);

ArrayList<Book> books = [Link]();

String all[] = new String[[Link]()];


for(int i=0; i<[Link](); i++)
{
all[i] = [Link](i).getName();
}

final ListView lv = (ListView)findViewById([Link]);

ArrayAdapter adapter = new ArrayAdapter(this, [Link].simple_list_item_1, all);


[Link](adapter);

final Intent bookDetailsIntent = new Intent(this, [Link]);

Button ok = (Button)findViewById([Link].list_ok_books);
[Link](new [Link]() {
@Override
public void onClick(View arg0) {
finish();
}
});
[Link](new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
String bookName = [Link](position).toString();
Log.d("bookName-->>>>>", bookName);
[Link]("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="[Link]

Lara Technologies [Link] 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>

[Link]
package [Link];

import [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class BookDetails extends Activity {


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].book_details);

final DataHelper dh = new DataHelper(this);


Intent intent = getIntent();
String bookName = (String) [Link]("bookName");
final Book book = [Link](bookName);

TextView name = (TextView)findViewById([Link].book_name_display);


TextView auther = (TextView)findViewById([Link].book_auther_display);
TextView price = (TextView)findViewById([Link].book_price_display);

[Link]([Link]());
[Link]([Link]());
[Link]([Link]());

Button ok = (Button)findViewById([Link].ok_book_details);

Lara Technologies [Link] 080-41310124


Button edit = (Button)findViewById([Link].edit_book_details);
Button delete = (Button)findViewById([Link].delete_book_details);

final Intent editIntent = new Intent(this, [Link]);


final Intent homeIntent = new Intent(this, [Link]);

[Link](new [Link]() {
@Override
public void onClick(View v) {
finish();
}
});

[Link](new [Link]() {
@Override
public void onClick(View v) {
[Link]("book", book);
startActivity(editIntent);
}
});

[Link](new [Link]() {
@Override
public void onClick(View v) {
[Link]([Link]());
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="[Link]
<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 [Link] 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>

[Link]
package [Link];

import [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class EditBook extends Activity {


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].edit_book);

final DataHelper dh = new DataHelper(this);

Intent intent = getIntent();


Book book = (Book)[Link]("book");

final EditText name = (EditText)findViewById([Link].book_name_edit);


final EditText auther = (EditText)findViewById([Link].book_auther_edit);
final EditText price = (EditText)findViewById([Link].book_price_edit);

Lara Technologies [Link] 080-41310124


[Link]([Link]());
[Link]([Link]());
[Link]([Link]());

Button cancel = (Button)findViewById([Link].cancel_edit_book);


Button save = (Button)findViewById([Link].save_edit_book);
final Intent addIntent = new Intent(this, [Link]);
final Intent homeIntent = new Intent(this, [Link]);

[Link](new [Link]() {
@Override
public void onClick(View arg0) {
Book book = new Book();
[Link]([Link]().toString());
[Link]([Link]().toString());
[Link]([Link]().toString());
[Link](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="[Link]
<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 [Link] 080-41310124

You might also like