18 SQLite Database Connectivity


activity_main.xml:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout tools:context=".MainActivity" 
 android:layout_height="match_parent" 
 android:layout_width="match_parent" 
 android:orientation="vertical" 
 xmlns:tools="http://schemas.android.com/tools" 
 xmlns:app="http://schemas.android.com/apk/res-auto" 
 xmlns:android="http://schemas.android.com/apk/res/android">
<EditText   
android:layout_height="wrap_content"
  android:layout_width="match_parent"    
  android:hint="Flower Name"
  android:inputType="textPersonName" 
  android:ems="10"     
  android:id="@+id/name"/>
<EditText  
  android:layout_height="wrap_content" 
  android:layout_width="match_parent" 
  android:hint="Flower Color"     
  android:inputType="textPersonName"   
  android:ems="10" 
  android:id="@+id/color"/> 
 
<Button 
 android:layout_height="wrap_content" 
 android:layout_width="match_parent" 
 android:id="@+id/save" 
 android:onClick="saveRecord" 
 android:text="save"/>
 
<Button 
 android:layout_height="wrap_content" 
 android:layout_width="match_parent" 
 android:id="@+id/load" 
 android:onClick="showRecord" 
 android:text="show"/>
 
<EditText  
 android:layout_height="wrap_content" 
 android:layout_width="match_parent" 
 android:hint="Enter ID" 
 android:inputType="textPersonName" 
 android:ems="10" 
 android:id="@+id/fid"/>
 
<Button 
 android:layout_height="wrap_content" 
 android:layout_width="match_parent" 
 android:id="@+id/update" 
 android:onClick="updateRecord" 
 android:text="update"/>
 
<Button 
 android:layout_height="wrap_content" 
 android:layout_width="match_parent" 
 android:id="@+id/delete" 
 android:onClick="deleteRecord" 
 android:text="delete"/>

</LinearLayout>

MainActivity.java

package com.example.basha.simplesqliteexample;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

    EditText name,color,id;
    MyDatabase  myDb;
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        name=findViewById(R.id.name);
        color=findViewById(R.id.color);
        id=findViewById(R.id.fid);
        myDb=new MyDatabase(this);  }

    public void saveRecord(View view) {
        myDb.doInsert(name.getText().toString(), color.getText().toString());  }

    public void showRecord(View view) {
        myDb.loadData();  }

    public void updateRecord(View view) {
        myDb.updateDate(Integer.parseInt(id.getText().toString()), name.getText().toString(), color.getText().toString());  }

    public void deleteRecord(View view) {
        myDb.delete(Integer.parseInt(id.getText().toString()));  }
}

MyDatabase.java:

package com.example.basha.simplesqliteexample;

import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.widget.Toast;

class MyDatabase extends SQLiteOpenHelper {
    static final private String DB_NAME="basket";
    private String TABLE_NAME="flowers";
    SQLiteDatabase myDb;
    Context ctx;
    public MyDatabase(Context context)
    {
        super(context,DB_NAME,null,1);
        ctx = context;
    }
    @Override    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table " + TABLE_NAME + "(_id integer primary key autoincrement,name text,color text);");
    }
    @Override    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        db.execSQL("drop table if exists " + TABLE_NAME);
        onCreate(db);

    }
    public void doInsert(String name,String color){
        myDb=getWritableDatabase();
        myDb.execSQL("insert into " + TABLE_NAME + " (name,color) values ('" +name+ "','"+color+"');");
        Toast.makeText(ctx, "Record Inserted", Toast.LENGTH_SHORT).show();
    }
    public void loadData()
    {
        myDb=getReadableDatabase();
        Cursor cursor =myDb.rawQuery("Select * from " + TABLE_NAME,null);

        String result= "";
        while(cursor.moveToNext()) {
            int id = cursor.getInt(0);
            String fname = cursor.getString(1);
            String fcolor = cursor.getString(2);

            result += id+ " " + fname + " " + fcolor+"\n";
        }
        Toast.makeText(ctx,result, Toast.LENGTH_SHORT).show();
    }

    public void updateDate(int id, String name, String color){
        myDb=getWritableDatabase();
        ContentValues content =new ContentValues();
        content.put("_id",id);
        content.put("name",name);
        content.put("color",color);

        String [] selectionArgs={String.valueOf(id)};

        myDb.update(TABLE_NAME, content, "_id=?",selectionArgs);
        Toast.makeText(ctx, "Record is Updated", Toast.LENGTH_SHORT).show();

    }

    public void delete(int id){
        myDb=getWritableDatabase();
        String [] selectionArgs={String.valueOf(id)};
        myDb.delete(TABLE_NAME, "_id=?",selectionArgs);

        Toast.makeText(ctx, "Record is Updated", Toast.LENGTH_SHORT).show();

    }
}

Comments

Popular posts from this blog

20 Simple Content Provider Example

14 Popup Menu

17 Simple Notification Program