SQLite
SQLite
It stores potent data storage to mobile and embedded applications without a track
It can read and write to general disk files openly in which a single disk file contains multiple tables,
indices, triggers, and views
The database file layout is cross-platform – that is, it can copy a database between 32 and 64 bit file
systems or between big endian and little endian architectures
The code of SQLite is open to use for any purpose, private or public.
It is a compressed library in which the features are enabled. The size of the library can be reduced
below 300KiB. It could be run in minimal stack space and heap thus making it popular to be used in
gadgets such as mobile phones, PDAs and MP3 players. The performance of SQLite is relatively good
in low memory level requirements.
Why SQLite?
Serverless: It is serverless which does not need a detach server process or system to operate.
Less memory: It is very small and light weight, less than 400 KiB completely configured or less than
250 KiB with optional features omitted.
Transaction: SQLite transactions support ACID properties to allow safe access from multiple
processes or threads.
Languages and OS: It supports most of the query language features found in the SQL 92 (SQL2)
standard. It is written in ANSI C and provides simple and easy to use API. SQLite is accessible on UNIX
and Windows.
Program
<?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:background="@android:color/holo_blue_dark">
<TextView
android:text="@string/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="12dp"
android:id="@+id/textView"
android:textSize="18sp"
android:textStyle="bold|italic"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:gravity="center" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/editName"
android:textStyle="bold|italic"
android:layout_below="@+id/textView"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:hint="Enter Name"
android:gravity="center_vertical|center" />
<TextView
android:text="@string/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="13dp"
android:id="@+id/textView2"
android:textStyle="bold|italic"
android:textSize="18sp"
android:layout_below="@+id/editName"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:gravity="center"
android:hint="Enter Password" />
<Button
android:text="@string/view_data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button2"
android:textSize="18sp"
android:onClick="viewdata"
android:textStyle="bold|italic"
android:layout_alignBaseline="@+id/button"
android:layout_alignBottom="@+id/button"
android:layout_alignRight="@+id/button4"
android:layout_alignEnd="@+id/button4" />
<Button
android:text="@string/add_user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:textStyle="bold|italic"
android:textSize="18sp"
android:onClick="addUser"
android:layout_marginLeft="28dp"
android:layout_marginStart="28dp"
android:layout_below="@+id/editPass"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="23dp" />
<Button
android:text="@string/update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button3"
android:onClick="update"
android:textStyle="normal|bold"
android:layout_below="@+id/editText3"
android:layout_alignLeft="@+id/button4"
android:layout_alignStart="@+id/button4"
android:layout_marginTop="13dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/editText6"
android:layout_alignTop="@+id/button4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:freezesText="false"
android:hint="Enter Name to Delete Data"
android:layout_toLeftOf="@+id/button2"
android:layout_toStartOf="@+id/button2" />
<Button
android:text="@string/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="21dp"
android:layout_marginEnd="21dp"
android:id="@+id/button4"
android:onClick="delete"
android:textStyle="normal|bold"
tools:ignore="RelativeOverlap"
android:layout_marginBottom="41dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_marginTop="47dp"
android:id="@+id/editText3"
android:textStyle="bold|italic"
android:textSize="14sp"
android:layout_below="@+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="7dp"
android:layout_marginStart="7dp"
android:hint="Current Name" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:layout_marginTop="11dp"
android:id="@+id/editPass"
android:hint="Enter Password"
android:gravity="center_vertical|center"
android:textSize="18sp"
android:layout_below="@+id/textView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textAllCaps="false"
android:textStyle="normal|bold" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/editText5"
android:textStyle="bold|italic"
android:textSize="14sp"
android:hint="New Name"
android:layout_alignTop="@+id/button3"
android:layout_alignLeft="@+id/editText3"
android:layout_alignStart="@+id/editText3"
android:layout_marginTop="32dp" />
</RelativeLayout>
package com.example.mydb;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
Toast.makeText(getApplicationContext(), data,
Toast.LENGTH_LONG).show();
}
Toast.makeText(getApplicationContext(), "Unsuccessful",
Toast.LENGTH_LONG).show();
updateold.setText("");
updatenew.setText("");
} else {
Toast.makeText(getApplicationContext(), "Updated",
Toast.LENGTH_LONG).show();
updateold.setText("");
updatenew.setText("");
}
}
}
public void delete( View view)
{
String uname = delete.getText().toString();
if(uname.isEmpty())
{
Toast.makeText(getApplicationContext(), "Unsuccessful",
Toast.LENGTH_LONG).show();
delete.setText("");
}
else
{
Toast.makeText(getApplicationContext(), "Deleted",
Toast.LENGTH_LONG).show();
delete.setText("");
}
}
}
}
package com.example.mydb;
import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Message;
import android.widget.Toast;
try {
db.execSQL(CREATE_TABLE);
} catch (Exception e) {
Toast.makeText(context.getApplicationContext(),
(CharSequence) e,Toast.LENGTH_LONG).show();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int
newVersion) {
try {
Toast.makeText(context.getApplicationContext(),
"OnUpgrade",Toast.LENGTH_LONG).show();
db.execSQL(DROP_TABLE);
onCreate(db);
}catch (Exception e) {
Toast.makeText(context.getApplicationContext(),
(CharSequence) e,Toast.LENGTH_LONG).show();
}
}
}
}
Description
This method needs the database files first, followed by the permissions to open the database with
an optional cursor factory builder.
There are some database properties that must be set after connecting to the database. They are;
1. If data is no longer required in the database, then we use the delete option to delete records
from the database
2. The delete() method expects 3 parameters;
Database name
Where clause
An argument array for the where clause
3. To delete all the records from the table, pass null for the where clause and where clause
argument array
4. Only we call the delete() method to remove records from SQLite database
5. The delete method contains the table name and optionally the where clause and anywhere
clause argument replacement arrays as parameters
6. The where clause and argument replacement array work just as with update where ? is
replaced by the values in the array
Cursors stores query result records in rows and grant many methods to access and iterate through
records
Cursors should be closed when no longer used and can be deactivated with a call to
Cursor.deactivate() statement when the application pauses or exits
On resume, the Cursor.requery() statement is executed to re enable the cursor with fresh data.
Transactions
When we want to execute a series of queries that either all complete or all fail, at that situation we
use transactions which is supported by SQLite
1. beginTransaction()
2. setTransactionSuccessful()
3. endTransaction()
Program
Start with a call to beginTransaction () to inform SQLite to execute the queries in transaction
mode.
Begin a try/catch mode to handle exceptions thrown by a transaction failure.
Perform the queries and then call setTransactionSuccessful() to inform SQLite that our
transaction is complete.
If an error isn’t thrown, then endTransaction() can be called to perform the transaction.
Finally, close the cursor when we are finished.