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

Android User Database App Code

The document contains code for an Android application that manages a SQLite database for user information, including functionality to insert, update, delete, and display users. It consists of a DatabaseHelper class for database operations and a MainActivity class for user interface interactions. The XML layout defines the user input fields and buttons for performing these operations.

Uploaded by

pranshavji
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views4 pages

Android User Database App Code

The document contains code for an Android application that manages a SQLite database for user information, including functionality to insert, update, delete, and display users. It consists of a DatabaseHelper class for database operations and a MainActivity class for user interface interactions. The XML layout defines the user input fields and buttons for performing these operations.

Uploaded by

pranshavji
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

//Database create app code

package [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class DatabaseHelper extends SQLiteOpenHelper {


private static final String DATABASE_NAME = "[Link]";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "Users";
private static final String COLUMN_ID = "id";
private static final String COLUMN_NAME = "name";
private static final String COLUMN_LASTNAME = "lastname";

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_NAME + " TEXT, " +
COLUMN_LASTNAME + " TEXT)";
[Link](createTable);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
[Link]("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}

public boolean insertUser(String name, String lastname) {


SQLiteDatabase db = [Link]();
ContentValues values = new ContentValues();
[Link](COLUMN_NAME, name);
[Link](COLUMN_LASTNAME, lastname);

long result = [Link](TABLE_NAME, null, values);


return result != -1;
}

public boolean updateUser(int id, String name, String lastname) {


SQLiteDatabase db = [Link]();
ContentValues values = new ContentValues();
[Link](COLUMN_NAME, name);
[Link](COLUMN_LASTNAME, lastname);

int result = [Link](TABLE_NAME, values, COLUMN_ID + "=?", new String[]


{[Link](id)});
return result > 0;
}

public boolean deleteUser(int id) {


SQLiteDatabase db = [Link]();
int result = [Link](TABLE_NAME, COLUMN_ID + "=?", new String[]
{[Link](id)});
return result > 0;
}

public Cursor getUsers() {


SQLiteDatabase db = [Link]();
return [Link]("SELECT * FROM " + TABLE_NAME, null);
}
}

//[Link] code
package [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class MainActivity extends AppCompatActivity {


DatabaseHelper dbHelper;
EditText edtId, edtName, edtLastname;
Button btnInsert, btnUpdate, btnDelete, btnShowData;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);

dbHelper = new DatabaseHelper(this);

edtId = findViewById([Link]);
edtName = findViewById([Link]);
edtLastname = findViewById([Link]);
btnInsert = findViewById([Link]);
btnUpdate = findViewById([Link]);
btnDelete = findViewById([Link]);
btnShowData = findViewById([Link]);

[Link](v -> insertUser());


[Link](v -> updateUser());
[Link](v -> deleteUser());
[Link](v -> showUsers());
}

private void insertUser() {


String name = [Link]().toString();
String lastname = [Link]().toString();

if ([Link]() || [Link]()) {
[Link](this, "Enter name and lastname",
Toast.LENGTH_SHORT).show();
return;
}
boolean inserted = [Link](name, lastname);
if (inserted) {
[Link](this, "User Inserted", Toast.LENGTH_SHORT).show();
} else {
[Link](this, "Insert Failed", Toast.LENGTH_SHORT).show();
}
}

private void updateUser() {


int id = [Link]([Link]().toString());
String name = [Link]().toString();
String lastname = [Link]().toString();

boolean updated = [Link](id, name, lastname);


if (updated) {
[Link](this, "User Updated", Toast.LENGTH_SHORT).show();
} else {
[Link](this, "Update Failed", Toast.LENGTH_SHORT).show();
}
}

private void deleteUser() {


int id = [Link]([Link]().toString());

boolean deleted = [Link](id);


if (deleted) {
[Link](this, "User Deleted", Toast.LENGTH_SHORT).show();
} else {
[Link](this, "Delete Failed", Toast.LENGTH_SHORT).show();
}
}

private void showUsers() {


Cursor cursor = [Link]();
if ([Link]() == 0) {
[Link](this, "No data found", Toast.LENGTH_SHORT).show();
return;
}

StringBuilder sb = new StringBuilder();


while ([Link]()) {
[Link]("ID: ").append([Link](0)).append("\n");
[Link]("Name: ").append([Link](1)).append("\n");
[Link]("Last Name: ").append([Link](2)).append("\n\n");
}

[Link] builder = new [Link](this);


[Link]("User Data")
.setMessage([Link]())
.setPositiveButton("OK", null)
.show();
}
}

//XML Code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
android:gravity="center">

<EditText
android:id="@+id/edtId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter ID (for update/delete)" />

<EditText
android:id="@+id/edtName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Name" />

<EditText
android:id="@+id/edtLastname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter Last Name" />

<Button
android:id="@+id/btnInsert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Insert User" />

<Button
android:id="@+id/btnUpdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Update User" />

<Button
android:id="@+id/btnDelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete User" />

<Button
android:id="@+id/btnShowData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Display" />
</LinearLayout>

You might also like