0% found this document useful (0 votes)
4 views

Databse create android 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)
4 views

Databse create android 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
You are on page 1/ 4

//Database create app code

package com.example.mydatabaseapp;

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

public class DatabaseHelper extends SQLiteOpenHelper {


private static final String DATABASE_NAME = "Users.db";
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)";
db.execSQL(createTable);
}

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

public boolean insertUser(String name, String lastname) {


SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_NAME, name);
values.put(COLUMN_LASTNAME, lastname);

long result = db.insert(TABLE_NAME, null, values);


return result != -1;
}

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


SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_NAME, name);
values.put(COLUMN_LASTNAME, lastname);

int result = db.update(TABLE_NAME, values, COLUMN_ID + "=?", new String[]


{String.valueOf(id)});
return result > 0;
}

public boolean deleteUser(int id) {


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

public Cursor getUsers() {


SQLiteDatabase db = this.getReadableDatabase();
return db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
}
}

//MainActivity.java code
package com.example.mydatabaseapp;

import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


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

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

dbHelper = new DatabaseHelper(this);

edtId = findViewById(R.id.edtId);
edtName = findViewById(R.id.edtName);
edtLastname = findViewById(R.id.edtLastname);
btnInsert = findViewById(R.id.btnInsert);
btnUpdate = findViewById(R.id.btnUpdate);
btnDelete = findViewById(R.id.btnDelete);
btnShowData = findViewById(R.id.btnShowData);

btnInsert.setOnClickListener(v -> insertUser());


btnUpdate.setOnClickListener(v -> updateUser());
btnDelete.setOnClickListener(v -> deleteUser());
btnShowData.setOnClickListener(v -> showUsers());
}

private void insertUser() {


String name = edtName.getText().toString();
String lastname = edtLastname.getText().toString();

if (name.isEmpty() || lastname.isEmpty()) {
Toast.makeText(this, "Enter name and lastname",
Toast.LENGTH_SHORT).show();
return;
}
boolean inserted = dbHelper.insertUser(name, lastname);
if (inserted) {
Toast.makeText(this, "User Inserted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Insert Failed", Toast.LENGTH_SHORT).show();
}
}

private void updateUser() {


int id = Integer.parseInt(edtId.getText().toString());
String name = edtName.getText().toString();
String lastname = edtLastname.getText().toString();

boolean updated = dbHelper.updateUser(id, name, lastname);


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

private void deleteUser() {


int id = Integer.parseInt(edtId.getText().toString());

boolean deleted = dbHelper.deleteUser(id);


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

private void showUsers() {


Cursor cursor = dbHelper.getUsers();
if (cursor.getCount() == 0) {
Toast.makeText(this, "No data found", Toast.LENGTH_SHORT).show();
return;
}

StringBuilder sb = new StringBuilder();


while (cursor.moveToNext()) {
sb.append("ID: ").append(cursor.getInt(0)).append("\n");
sb.append("Name: ").append(cursor.getString(1)).append("\n");
sb.append("Last Name: ").append(cursor.getString(2)).append("\n\n");
}

AlertDialog.Builder builder = new AlertDialog.Builder(this);


builder.setTitle("User Data")
.setMessage(sb.toString())
.setPositiveButton("OK", null)
.show();
}
}

//XML Code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy