SQLite Connection for Android Studio

Praneet Dighe
3 min readMay 8, 2023

--

DBHandler.java

package com.example.project;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHandler extends SQLiteOpenHelper {

private static final String DATABASE_NAME = "user_details.db";
private static final int DATABASE_VERSION = 1;

public DBHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
// Create the UserDetails table
db.execSQL(UserDetails.CREATE_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop the UserDetails table if it exists
db.execSQL(UserDetails.DROP_TABLE);
// Create a new UserDetails table
}

public class UserDetails {

public static final String TABLE_NAME = "UserDetails";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_FIRST_NAME = "first_name";
public static final String COLUMN_LAST_NAME = "last_name";
public static final String COLUMN_PHONE = "phone";
public static final String COLUMN_EMAIL = "email";
public static final String COLUMN_AREA = "area";
public static final String COLUMN_PIN_CODE = "pin_code";
public static final String COLUMN_BUILDING_NAME = "building_name";
public static final String COLUMN_PASSWORD = "password";

public static final String CREATE_TABLE =
"CREATE TABLE " + TABLE_NAME + "("
+ COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ COLUMN_FIRST_NAME + " TEXT,"
+ COLUMN_LAST_NAME + " TEXT,"
+ COLUMN_PHONE + " TEXT,"
+ COLUMN_EMAIL + " TEXT,"
+ COLUMN_AREA + " TEXT,"
+ COLUMN_PIN_CODE + " TEXT,"
+ COLUMN_BUILDING_NAME + " TEXT,"
+ COLUMN_PASSWORD + " TEXT"
+ ")";

public static final String DROP_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME;

}

}

SignupActivity.java

package com.example.project;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SignUpActivity extends AppCompatActivity {

private EditText firstNameEditText;
private EditText lastNameEditText;
private EditText phoneEditText;
private EditText emailEditText;
private EditText areaEditText;
private EditText pinCodeEditText;
private EditText buildingNameEditText;
private EditText passwordEditText;
private Button signUpButton;

private SQLiteDatabase db;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
// Initialize views
firstNameEditText = findViewById(R.id.firstNameEditText);
lastNameEditText = findViewById(R.id.lastNameEditText);
phoneEditText = findViewById(R.id.phoneNumberEditText);
emailEditText = findViewById(R.id.emailEditText);
areaEditText = findViewById(R.id.areaEditText);
pinCodeEditText = findViewById(R.id.pinCodeEditText);
buildingNameEditText = findViewById(R.id.buildingNameEditText);
passwordEditText = findViewById(R.id.passwordEditText);
signUpButton = findViewById(R.id.signUpButton);

DBHandler dbHelper = new DBHandler(this);
db = dbHelper.getWritableDatabase();

signUpButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String firstName = firstNameEditText.getText().toString().trim();
String lastName = lastNameEditText.getText().toString().trim();
String phone = phoneEditText.getText().toString().trim();
String email = emailEditText.getText().toString().trim();
String area = areaEditText.getText().toString().trim();
String pinCode = pinCodeEditText.getText().toString().trim();
String buildingName = buildingNameEditText.getText().toString().trim();
String password = passwordEditText.getText().toString().trim();

// Insert user details into the database
ContentValues values = new ContentValues();
values.put(DBHandler.UserDetails.COLUMN_FIRST_NAME, firstName);
values.put(DBHandler.UserDetails.COLUMN_LAST_NAME, lastName);
values.put(DBHandler.UserDetails.COLUMN_PHONE, phone);
values.put(DBHandler.UserDetails.COLUMN_EMAIL, email);
values.put(DBHandler.UserDetails.COLUMN_AREA, area);
values.put(DBHandler.UserDetails.COLUMN_PIN_CODE, pinCode);
values.put(DBHandler.UserDetails.COLUMN_BUILDING_NAME, buildingName);
values.put(DBHandler.UserDetails.COLUMN_PASSWORD, password);
db.insert(DBHandler.UserDetails.TABLE_NAME, null, values);

Toast.makeText(SignUpActivity.this, "User details saved", Toast.LENGTH_SHORT).show();

// Navigate to the user dashboard activity
Intent intent = new Intent(SignUpActivity.this, UserDashboardActivity.class);
startActivity(intent);
}

});
}
}

activity_sign_up.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"
tools:context=".SignUpActivity">

<EditText
android:id="@+id/firstNameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="First Name"
android:layout_marginTop="50dp"
android:textColorHint="#999999"/>

<EditText
android:id="@+id/lastNameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Last Name"
android:layout_below="@+id/firstNameEditText"
android:layout_marginTop="20dp"
android:textColorHint="#999999"/>

<EditText
android:id="@+id/phoneNumberEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Phone Number"
android:inputType="phone"
android:layout_below="@+id/lastNameEditText"
android:layout_marginTop="20dp"
android:textColorHint="#999999"/>

<EditText
android:id="@+id/emailEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email"
android:inputType="textEmailAddress"
android:layout_below="@+id/phoneNumberEditText"
android:layout_marginTop="20dp"
android:textColorHint="#999999"/>

<EditText
android:id="@+id/areaEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Area"
android:layout_below="@+id/emailEditText"
android:layout_marginTop="20dp"
android:textColorHint="#999999"/>

<EditText
android:id="@+id/pinCodeEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Pin Code"
android:inputType="number"
android:layout_below="@+id/areaEditText"
android:layout_marginTop="20dp"
android:textColorHint="#999999"/>

<EditText
android:id="@+id/buildingNameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Building Name"
android:layout_below="@+id/pinCodeEditText"
android:layout_marginTop="20dp"
android:textColorHint="#999999"/>

<EditText
android:id="@+id/passwordEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword"
android:layout_below="@+id/buildingNameEditText"
android:layout_marginTop="20dp"
android:textColorHint="#999999"/>

<EditText
android:id="@+id/passwordEditText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/buildingNameEditText"
android:layout_marginTop="91dp"
android:hint="Reenter Password"
android:inputType="textPassword"
android:textColorHint="#999999" />

<Button
android:id="@+id/signUpButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sign Up"
android:layout_below="@+id/passwordEditText2"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"/>

</RelativeLayout>

--

--