0% found this document useful (0 votes)
85 views4 pages

Practical-13: Create Application Which Show Use of Sqlite Database

This document describes an Android application that uses a SQLite database to store and retrieve book data. The application creates a database with a table to hold book names and authors. It allows the user to insert, delete, and view all records. The main activity class handles database operations like creating the table, inserting, deleting and querying records to display on screen. The activity layout contains fields and buttons to add/remove data and display the results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views4 pages

Practical-13: Create Application Which Show Use of Sqlite Database

This document describes an Android application that uses a SQLite database to store and retrieve book data. The application creates a database with a table to hold book names and authors. It allows the user to insert, delete, and view all records. The main activity class handles database operations like creating the table, inserting, deleting and querying records to display on screen. The activity layout contains fields and buttons to add/remove data and display the results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Practical 13

Practical-13

Aim: Create application which show use of sqlite database.

//AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.arpit.p13">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


</intent-filter>
</activity>
</application>

</manifest>

//MainActivity.java
package com.example.arpit.p13;

import android.content.ContentValues; import android.database.Cursor;


import android.database.sqlite.SQLiteDatabase; import
android.support.v7.app.AppCompatActivity; import android.os.Bundle;
import android.view.View; import android.widget.EditText; import
android.widget.TextView; import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

SQLiteDatabase mdb;
EditText book,author;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
mdb = openOrCreateDatabase("books.db",
SQLiteDatabase.CREATE_IF_NECESSARY, null);
mdb.setVersion(1);
final String CREATE_BOOK = "create table books(id integer primary key
autoincrement,name text,author text)";
mdb.execSQL(CREATE_BOOK);
}
catch ( Exception e)
{
Toast.makeText(this, "error "+e.getMessage(),
Toast.LENGTH_SHORT).show();
}
book=(EditText)findViewById(R.id.etbook);
author=(EditText)findViewById(R.id.etauthor);
}

NAME: PRADIP J. METHANIYA


ENROLLMENT_NO: 14012011026 Page 56
Practical 13

public void showRec(View view) {


Cursor c=mdb.query("books",null,null,null,null,null,null);
startManagingCursor(c);
String rowhead=" || "; String res=" || ";
for (int i=0;i<c.getColumnCount();i++)
rowhead=rowhead.concat(c.getColumnName(i)+" || ");
c.moveToFirst();
while (c.isAfterLast()==false)
{
for (int i=0;i<c.getColumnCount();i++)
{
res=res.concat(c.getString(i)+" || ");
}
res.concat("\n \n"); c.moveToNext();
}
TextView txt=(TextView)findViewById(R.id.textView);
txt.setText(rowhead+"\n"+res);
}

public void insertbook(View view) {


try {
ContentValues values = new ContentValues();
values.put("name",book.getText().toString());
values.put("author",author.getText().toString());
long newbookid = mdb.insertOrThrow("books", null, values);
book.setText(null);
author.setText(null);
Toast.makeText(this, "inserted: "+newbookid,Toast.LENGTH_LONG).show();
showRec(view);
}
catch ( Exception e){
Toast.makeText(this, "error "+e.getMessage(),
Toast.LENGTH_SHORT).show();
}
}

public void deletebook(View view) {


try {

ContentValues values = new ContentValues();


values.put("name", book.getText().toString());
mdb.delete("books", "name=?", new String[]{book.getText().toString()});
Toast.makeText(this, "book " + book.getText() + " deleted ",
Toast.LENGTH_SHORT).show();
showRec(view);
}
catch (Exception e)
{
Toast.makeText(this, "error "+e.getMessage(),
Toast.LENGTH_SHORT).show();
}

}
}

//activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"

NAME: PRADIP J. METHANIYA


ENROLLMENT_NO: 14012011026 Page 57
Practical 13

tools:context="com.example.arpit.p13.MainActivity">

<Button
android:text="Show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnshow"
android:onClick="showRec"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="183dp" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="data"
android:id="@+id/textView"
android:lines="10"
android:layout_marginTop="93dp"
android:layout_alignTop="@+id/btnshow"
android:layout_centerHorizontal="true" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_alignParentEnd="true"
android:layout_marginEnd="15dp"
android:id="@+id/etbook"
android:layout_alignParentRight="true"
android:layout_marginRight="15dp" />

<TextView android:text="book name"


android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:id="@+id/textView2"
android:layout_alignBottom="@+id/etbook"
android:layout_alignParentStart="true"
android:layout_marginLeft="30dp"
android:layout_alignParentLeft="true" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_marginTop="48dp"
android:id="@+id/etauthor"
android:layout_below="@+id/etbook"
android:layout_alignLeft="@+id/etbook"
android:layout_alignStart="@+id/etbook" />

<Button
android:text="Insert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toStartOf="@+id/textView3"
android:id="@+id/btnins"
android:onClick="insertbook"
android:layout_alignBottom="@+id/btnshow"
android:layout_alignRight="@+id/textView2"
android:layout_alignEnd="@+id/textView2" />

<Button
android:text="Delete"
android:layout_width="wrap_content"

NAME: PRADIP J. METHANIYA


ENROLLMENT_NO: 14012011026 Page 58
Practical 13

android:layout_height="wrap_content"
android:id="@+id/btndelete"
android:layout_alignBaseline="@+id/btnshow"
android:layout_alignBottom="@+id/btnshow"
android:layout_alignEnd="@+id/etbook"
android:onClick="deletebook"/>

<TextView
android:text="author"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView3"
android:layout_alignBaseline="@+id/etauthor"
android:layout_alignBottom="@+id/etauthor"
android:layout_alignLeft="@+id/textView2"
android:layout_alignStart="@+id/textView2" />

</RelativeLayout>

OUTPUT:-

NAME: PRADIP J. METHANIYA


ENROLLMENT_NO: 14012011026 Page 59

You might also like