0% found this document useful (0 votes)
42 views6 pages

Apd 8

The document describes steps to develop a login application using a SQLite database in Android. It involves setting up layout files, creating a SQLite database helper class to manage the database, developing login and welcome activities, and implementing database operations and login/signup logic in the login activity. The code provided creates the database tables, adds and authenticates users. The application allows users to login if authenticated or signup as a new user if login fails.
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)
42 views6 pages

Apd 8

The document describes steps to develop a login application using a SQLite database in Android. It involves setting up layout files, creating a SQLite database helper class to manage the database, developing login and welcome activities, and implementing database operations and login/signup logic in the login activity. The code provided creates the database tables, adds and authenticates users. The application allows users to login if authenticated or signup as a new user if login fails.
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/ 6

EX NO: 8 DEVELOP A LOGIN APPLICATION USING SQLITE DATABASE

AIM:
To develop a login application using sqlite database

STEPS:
Step 1: Set up the Layout
- Create XML layout files for the login and welcome pages.

Step 2: Create SQLite Database Helper


- Create a SQLite database helper class for managing database operations.

Step 3: Create Login Activity


- Develop a LoginActivity to handle login and signup actions.

Step 4: Create Welcome Activity


- Create a WelcomeActivity to display the welcome message.

Step 5: Implement Database Operations


- Implement database operations and login/signup logic in LoginActivity.

CODE:

ACTIVITY_MAIN.XML

<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout 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"
android:padding="16dp"
tools:context=".MainActivity">

<EditText
android:id="@+id/usernameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:inputType="text" />

<EditText
android:id="@+id/passwordEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"

21CSE008 HARINI S
android:layout_below="@id/usernameEditText"
android:hint="Password"
android:inputType="textPassword" />

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/passwordEditText"
android:text="Login"
android:onClick="loginClick" />

</RelativeLayout>
MAINACTIVITY.JAVA

package com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class MainActivity extends AppCompatActivity {


private EditText usernameEditText;
private EditText passwordEditText;
private DatabaseHelper dbHelper;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

usernameEditText = findViewById(R.id.usernameEditText);
passwordEditText = findViewById(R.id.passwordEditText);
dbHelper = new DatabaseHelper(this);
}

public void loginClick(View view) {


String username = usernameEditText.getText().toString();
String password = passwordEditText.getText().toString();
if (username.isEmpty() || password.isEmpty()) {
Toast.makeText(this, "Username and password are required.",
Toast.LENGTH_SHORT).show();
} else if (authenticate(username, password)) {

21CSE008 HARINI S
Toast.makeText(this, "Login Successful", Toast.LENGTH_SHORT).show();

} else {
// Failed login, attempt sign-up
long result = dbHelper.addUser(username, password);
if (result != -1) {
Toast.makeText(this, "Sign-up Successful", Toast.LENGTH_SHORT).show();

} else {
Toast.makeText(this, "Sign-up Failed", Toast.LENGTH_SHORT).show();
}
}
}
private boolean authenticate(String username, String password) {

SQLiteDatabase db = dbHelper.getReadableDatabase();
String[] columns = {DatabaseHelper.COLUMN_ID};
String selection = DatabaseHelper.COLUMN_USERNAME + " = ? AND " +
DatabaseHelper.COLUMN_PASSWORD + " = ?";
String[] selectionArgs = {username, password};
Cursor cursor = db.query(DatabaseHelper.TABLE_NAME, columns, selection,
selectionArgs, null, null, null);
int count = cursor.getCount();
cursor.close();
db.close();

return count > 0;


}
}

DATABASEHELPER.JAVA
package com.example.myapplication;
import android.content.Context;
import android.database.sqlite.SQLiteOpenHelper;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {


private static final String DATABASE_NAME = "login.db";
private static final int DATABASE_VERSION = 1;
public static final String TABLE_NAME = "users";

21CSE008 HARINI S
public static final String COLUMN_ID = "id";
public static final String COLUMN_USERNAME = "username";
public static final String COLUMN_PASSWORD = "password";

public DatabaseHelper(Context context) {


super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
String createTable = "CREATE TABLE " + TABLE_NAME + " ("
+ COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ COLUMN_USERNAME + " TEXT, "
+ COLUMN_PASSWORD + " TEXT)";
db.execSQL(createTable);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public long addUser(String username, String password) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_USERNAME, username);
values.put(COLUMN_PASSWORD, password);
long newRowId = db.insert(TABLE_NAME, null, values);
db.close();
return newRowId;
}
}

21CSE008 HARINI S
OUTPUT:

21CSE008 HARINI S
RESULT:

Thus the login application was created successfullt using sqlite

21CSE008 HARINI S

You might also like