Databse create android code
Databse create android 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;
@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);
}
//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;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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);
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();
}
}
//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>