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

Practical 26

The document describes creating an Android application that inserts user inputted name and post data into a SQLite database table. It includes XML and Java code for the user interface, database helper class, and activity class to handle input, database operations, and outputting results.

Uploaded by

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

Practical 26

The document describes creating an Android application that inserts user inputted name and post data into a SQLite database table. It includes XML and Java code for the user interface, database helper class, and activity class to handle input, database operations, and outputting results.

Uploaded by

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

Practical 26

Q1)
XML file
<?xml version="1.0" encoding="utf-8" ?>
<RelativeLayout
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
tools:context=".MainActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp"
>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter name "
android:layout_centerHorizontal="true"
android:layout_marginTop="200dp"
android:id="@+id/edt1"
android:minHeight="48dp"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter Post "
android:layout_marginTop="40dp"
android:layout_centerHorizontal="true"
android:layout_below="@id/edt1"
android:id="@+id/edt2"
android:minHeight="48dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn1"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dp"
android:text="insert"
android:textSize="20dp"
android:layout_below="@id/edt2"
/>

</RelativeLayout>

JAVA FILE
MainActivity.java
package com.example.practical261;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Dialog;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {

EditText name;
EditText post;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = findViewById(R.id.edt1);
post = findViewById(R.id.edt2);

MySqliteDemo database = new MySqliteDemo(MainActivity.this);


Button btn1 = findViewById(R.id.btn1);
btn1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view)
{
if(name.getText().toString().equals(" ") && post.getText().toString().equals(" ")) {
Toast.makeText(getApplicationContext(), "Please Enter value in every
Field",Toast.LENGTH_SHORT).show();
}
else{
long val = database.insert(name.getText().toString(),post.getText().toString());
if (val > 0) {
Toast.makeText(getApplicationContext(), val + " Values is inserted
successfully", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getApplicationContext(), val + " insertion is not successfully",
Toast.LENGTH_SHORT).show();

}
}
}
});
}
}
MySqliteDemo.java
package com.example.practical261;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class MySqliteDemo extends SQLiteOpenHelper {


private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "EmployeeDetails";
private static final String TABLE_NAME = "EmployeeInfo";
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_POST = "job";

public MySqliteDemo(Context context) {


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

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME + "(" + KEY_ID + " INTEGER
PRIMARY KEY AUTOINCREMENT ," + KEY_NAME + " TEXT ,"+ KEY_POST+" TEXT );" );
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public long insert(String name, String post) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues content = new ContentValues();
content.put(KEY_NAME, name);
content.put(KEY_POST, post);
return db.insert(TABLE_NAME, null, content);
}
}
OUTPUT:-
PATH TO EXPLOER DATABASE

MOBILE OUPUT

You might also like