Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

Practical No: 09

Aim:To study the database in android.

Code:
package com.example.simplesql;

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

import android.database.Cursor;

import android.database.sqlite.SQLiteDatabase;

import android.widget.*;

public class MainActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

TextView textView = (TextView)findViewById(R.id.textView1);

SQLiteDatabase database =
openOrCreateDatabase("shweta.db",MODE_PRIVATE,null);

database.execSQL("create table if not exists sampletable(name text,location


text)");

database.execSQL("insert into sampletable values('shweta','jalna')");

Cursor cursor = database.rawQuery("select * from sampletable", null);

cursor.moveToFirst();

String name = cursor.getString(0);

String location = cursor.getString(1);

textView.setText(name + "\n" + location);

database.close();

}
@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

Output:

Practical No: 09 Class of Student: SE / TE Student Roll


/ BE Number:3603
Name of Student: Dahatonde shweta G.
Sign with Date of Faculty: Grade

You might also like