package com.example.dml;
import java.util.Locale;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private SQLiteDatabase db;
Button up;
private String table_name="StudentInfo";
private String database_name="Test.db";
private Object Button;
public static final String KEY_ROWID = "sid";
public static final String KEY_NAME = "name";
public static final String KEY_AGE = "age";
public static final String KEY_COURSE = "course";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = openOrCreateDatabase(database_name , SQLiteDatabase.CREATE_IF_NECESSARY,
null);
db.setVersion(1);
db.setLocale(Locale.getDefault());
db.setLockingEnabled(true);
// creating table in database
db.execSQL("CREATE TABLE IF NOT EXISTS "+table_name+" " +
"( sid INTEGER PRIMARY KEY AUTOINCREMENT," +
" name TEXT," +
" age INTEGER," +
" course TEXT ); ");
}
public void onClick(View v)
{
switch(v.getId())
{
case R.id.btnInsert
:
Insert("Rohit", "19", "BBA");
break;
case R.id.btnUpdat:
Update(1, "Rohit", "20", "BCA");
break;
case R.id.btnDelete:
Delete(1);
break;
}
}
public void Insert(String name, String age, String course)
{
ContentValues data=createContentValues(name, age, course);
db.insert(table_name, null, data);
Toast.makeText(this, "Record Inserted", Toast.LENGTH_SHORT).show();
}
// updating record in the database
public void Update(int sid, String name, String age, String course)
{
ContentValues updateValues = createContentValues(name, age, course);
db.update(table_name, updateValues, KEY_ROWID+"="+sid, null);
Toast.makeText(this, "Record Updated", Toast.LENGTH_SHORT).show();
}
// deleting record rom the database
public void Delete(int sid)
{
db.delete(table_name, KEY_ROWID+"="+sid, null);
Toast.makeText(this, "Record Deleted", Toast.LENGTH_SHORT).show();
}
// return a content of the database
private ContentValues createContentValues(String name, String age, String course)
{
ContentValues values = new ContentValues();
values.put(KEY_NAME, name);
values.put(KEY_AGE, age);
values.put(KEY_COURSE, course);
return values;
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button android:id="@+id/btnInsert"
android:text="Insert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"/>
<Button
android:id="@+id/btnDelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btnInsert"
android:layout_below="@+id/btnInsert"
android:layout_marginLeft="24dp"
android:layout_marginTop="107dp"
android:onClick="onClick"
android:text="Delete" />
<Button
android:id="@+id/btnUpdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btnInsert"
android:layout_marginLeft="57dp"
android:layout_toRightOf="@+id/btnDelete"
android:onClick=""
android:text="Update" />
</RelativeLayout>