Ex.No:1 To create a database table and to display the database table field using SQLite Database in Android Studio.
To create a database table and to display the database table field using SQLite Database in Android Studio.
Android Studio(Min.required Artic Fox)
Step 1: Open Android Stdio and then click on File -> New -> New project.
Step 2: Then type the Application name as HelloWorld and click Next.
Step 3: Then select the Minimum SDK as shown below and click Next.
Step 4: Then select the Empty Activity and click Next. Finally click Finish.
Step 5: Design layout in activity_main.xml.
Step 6: Display message give in MainActivity file.
Step 7: Save and run the application.
/* Program to print the DatabaseTable using the SQLite”.
*/
package com.example.sandeep;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
EditText editUserID;
EditText editUserName;
EditText editUserPassword;
DatabaseManager dbManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editUserID= findViewById(R.id.editTextID);
editUserName=findViewById(R.id.editTextUserName);
editUserPassword=findViewById(R.id.editTextPassword);
dbManager=new DatabaseManager(this);
try{
dbManager.open();
}
catch(Exception e){
e.printStackTrace();
}
}
public void btnInsertPressed(View v){
dbManager.insert(editUserName.getText().toString(),editUserPassword.getText().toString());
}
public void btnFetchPressed(View v){
Cursor cursor=dbManager.fetch();
if (cursor.moveToFirst())
{
do{
@SuppressLint("Range") String ID=cursor.getString(cursor.getColumnIndex(DatabaseHelper.USER_ID));
@SuppressLint("Range") String username=cursor.getString(cursor.getColumnIndex(DatabaseHelper.USER_NAME));
@SuppressLint("Range") String password=cursor.getString(cursor.getColumnIndex(DatabaseHelper.USER_PASSWORD));
Log.i("DATABASE_TAG","I have read ID : "+ID+" Username : "+username+" password : "+password);
}while(cursor.moveToNext());
}
}
public void btnUpdatePressed(View v){
dbManager.update(Long.parseLong(editUserID.getText().toString()),editUserName.getText().toString(),editUserPassword.getText().toString());
}
public void btnDeletePressed(View v){
dbManager.delete(Long.parseLong(editUserID.getText().toString()));
}
}
package com.example.sandeep;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
static final String DATABASE_NAME="DataBASE.DB";
static final int DATABASE_VERSION=1;
static final String DATABASE_TABLE= "USERS";
static final String USER_ID= "_ID";
static final String USER_NAME="user_name";
static final String USER_PASSWORD="password";
private static final String CREATE_DB_QUERY = "CREATE TABLE "+DATABASE_TABLE +" ( "+ USER_ID+ " INTEGER PRIMARY KEY AUTOINCREMENT, "+USER_NAME+ " TEXT NOT NULL, "+ USER_PASSWORD+ " TEXT NOT NULL );";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_DB_QUERY);
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP TABLE IF EXISTS "+ DATABASE_TABLE);
}
}
package com.example.sandeep;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.sql.SQLDataException;
public class DatabaseManager {
private DatabaseHelper dbHelper;
private Context context;
private SQLiteDatabase database;
public DatabaseManager(Context ctx){
context=ctx;
}
public DatabaseManager open() throws SQLDataException {
dbHelper = new DatabaseHelper(context);
database = dbHelper.getWritableDatabase();
return this;
}
public void close(){
dbHelper.close();
}
public void insert (String username, String password){
ContentValues contentValues=new ContentValues();
contentValues.put(DatabaseHelper.USER_NAME,username);
contentValues.put(DatabaseHelper.USER_PASSWORD,password);
database.insert(DatabaseHelper.DATABASE_TABLE,null,contentValues);
}
public Cursor fetch(){
String [] columns= new String[]{DatabaseHelper.USER_ID,DatabaseHelper.USER_NAME,DatabaseHelper.USER_PASSWORD};
Cursor cursor=database.query(DatabaseHelper.DATABASE_TABLE,columns,null,null,null,null,null);
if(cursor!=null){
cursor.moveToFirst();
}
return cursor;
}
public int update(long _id,String username,String password){
ContentValues contentValues=new ContentValues();
contentValues.put(DatabaseHelper.USER_NAME,username);
contentValues.put(DatabaseHelper.USER_PASSWORD,password);
int ret = database.update(DatabaseHelper.DATABASE_TABLE,contentValues,DatabaseHelper.USER_ID+"="+_id,null);
return ret;
}
public void delete(long id){
database.delete(DatabaseHelper.DATABASE_TABLE,DatabaseHelper.USER_ID,null);
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/editTextUserName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:ems="10"
android:inputType="textPersonName"
android:minHeight="48dp"
android:hint="User Name"
android:textAlignment="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editTextID" />
<EditText
android:id="@+id/editTextID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:hint="User ID"
android:textAlignment="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.144" />
<EditText
android:id="@+id/editTextPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="User Password"
android:inputType="textPassword"
android:textAlignment="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editTextUserName" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="btnFetchPressed"
android:text="Fetch"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.475" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="btnInsertPressed"
android:text="Insert"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.126"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.475" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="btnUpdatePressed"
android:text="Update"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.896"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.475" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="btnDeletePressed"
android:text="Delete"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.623" />
</androidx.constraintlayout.widget.ConstraintLayout>
Thus a Simple Android Application create a dtatabase table and to display the database table using SQLite Database in Android Studio is developed and executed successfully.