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

sqlite_final

The document contains an Android layout and Java code for a simple application that allows users to input student names into a SQLite database. It includes an EditText for input, two buttons for submitting and displaying records, and a TextView for showing the stored names. The application creates a database if it doesn't exist and handles user interactions to store and retrieve data.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

sqlite_final

The document contains an Android layout and Java code for a simple application that allows users to input student names into a SQLite database. It includes an EditText for input, two buttons for submitting and displaying records, and a TextView for showing the stored names. The application creates a database if it doesn't exist and handles user interactions to store and retrieve data.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

<?xml version="1.0" encoding="utf-8"?

>
<androidx.constraintlayout.widget.ConstraintLayout
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=".MainActivity">

<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="submit"
app:layout_constraintBottom_toTopOf="@+id/button3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText2" />

<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="show"
app:layout_constraintBottom_toTopOf="@+id/textView1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText2"
app:layout_constraintVertical_bias="0.778" />

<TextView
android:id="@+id/textView1"
android:layout_width="259dp"
android:layout_height="212dp"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.585"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.911" />

</androidx.constraintlayout.widget.ConstraintLayout>

package com.example.sqlite_insert;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;

import android.database.sqlite.SQLiteDatabase;
import android.text.Editable;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {


SQLiteDatabase Student_database;

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

final EditText eroll = (EditText)findViewById(R.id.editText2);

Button bsubmit = (Button)findViewById(R.id.button1);

Button bshow_details = (Button)findViewById(R.id.button3);


final TextView tvstuddetails =
(TextView)findViewById(R.id.textView1);

String s;

bsubmit.setOnClickListener(new OnClickListener() {

public void onClick(View arg0) {


Student_database = openOrCreateDatabase("student",
Context.MODE_PRIVATE,null);

//openOrCreateDatabase("student",null);
Student_database.execSQL("CREATE TABLE IF NOT EXISTS
stud(name VARCHAR(20));");
String s, s1;

s1 = eroll.getText().toString();

Student_database.execSQL("INSERT INTO stud VALUES('" + s1 +


"');");
Toast.makeText(getApplicationContext()," Record
successfully Stored",Toast.LENGTH_LONG).show();

Student_database.close();
}
});

bshow_details.setOnClickListener(new OnClickListener() {

public void onClick(View arg0) {

Student_database = openOrCreateDatabase("student",
Context.MODE_PRIVATE,null);
String s6;
int total = 0, n;

Cursor C3=Student_database.rawQuery("select * from


stud;",null);
C3.moveToFirst();
if(!C3.isAfterLast())
{
do{
s6 = C3.getString(0);
tvstuddetails.append("\n" + s6);

}while(C3.moveToNext());
}

Student_database.close();

}
});

You might also like