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

android program Database connectivity

The document contains an Android program that establishes database connectivity using SQLite. It includes an XML layout with an EditText for input, two buttons for submitting and retrieving records, and a TextView for displaying results. The Java code handles button clicks to insert data into a database and retrieve records from it, displaying them in the TextView.

Uploaded by

solankesnehal96k
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

android program Database connectivity

The document contains an Android program that establishes database connectivity using SQLite. It includes an XML layout with an EditText for input, two buttons for submitting and retrieving records, and a TextView for displaying results. The Java code handles button clicks to insert data into a database and retrieve records from it, displaying them in the TextView.

Uploaded by

solankesnehal96k
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

//android program Database connectivity

Xml file

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


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/t1"
android:text=" "/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b1"
android:text="SUBMIT"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/b2"
android:text="GET RECORD"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/l1"
android:text="list"/>
</LinearLayout>
package com.example.datebasedemo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements


View.OnClickListener {

Button b1, b2;


EditText t1;
TextView l1;
SQLiteDatabase md;
ContentValues values;

protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.myfile);
b1 = (Button) findViewById(R.id.b1);
b2 = (Button) findViewById(R.id.b2);
t1 = (EditText) findViewById(R.id.t1);
l1 = (TextView) findViewById(R.id.l1);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
md = openOrCreateDatabase("mydb", MODE_PRIVATE, null);
md.execSQL("create table student10(name Text)");
values=new ContentValues();

public void onClick(View v) {


try {
if (v.getId() == R.id.b1)

values.put("name",t1.getText().toString());
md.insert("student10",null,values);

Toast.makeText(this, "record inserted", Toast.LENGTH_LONG).show();


}
if (v.getId() == R.id.b2) {

Cursor c = md.rawQuery("SELECT * FROM student10",null);


while(c.moveToNext()) {
String name = c.getString(0);
l1.append("\n" + name);
}
}

}
catch (Exception e)
{
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}

You might also like