Android database example




Manifest File

xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="myapplication.example1.com.hackonlycom"


    >
    <uses-sdk        android:minSdkVersion="15"        android:targetSdkVersion="26" />
    <uses-permission android:name= "android.permission.SET_WALLPAPER" />
<uses-permission android:name="android.permission.INTERNET"/>

    <application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:roundIcon="@mipmap/ic_launcher_round"        android:supportsRtl="true"        android:theme="@style/AppTheme"
        >

        <activity android:name=".SqlLiteExample">


            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".HotOrNot" />
        <activity android:name=".SQLLiteView" />



    </application>

</manifest>


SQLLiteExample.java

package myapplication.example1.com.hackonlycom;


import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import myapplication.example1.com.hackonlycom.HotOrNot;
public class SqlLiteExample extends Activity implements OnClickListener {
    Button sqlUpdate, sqlView, sqlModify, sqlGetInfo, sqlDelete;
    EditText sqlName, sqlHotness, sqlRow;

    @Override    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub        super.onCreate(savedInstanceState);
        setContentView(R.layout.sqlliteexample);
        sqlUpdate=(Button)findViewById(R.id.bSQLUpdate);
        sqlName=(EditText)findViewById(R.id.etSQLName);
        sqlHotness=(EditText)findViewById(R.id.etSQLHotness);
        sqlView=(Button)findViewById(R.id.bSQLOpenView);
        sqlRow=(EditText) findViewById(R.id.etSQLrawInfo);
        sqlDelete=(Button)findViewById(R.id.bSQLdelete);
        sqlModify=(Button)findViewById(R.id.bSQLmodify);
        sqlGetInfo=(Button)findViewById(R.id.bgetInfo);
        sqlView.setOnClickListener(this);
        sqlUpdate.setOnClickListener(this);
        sqlGetInfo.setOnClickListener(this);
        sqlModify.setOnClickListener(this);
        sqlDelete.setOnClickListener(this);

    }
    @Override    public void onClick(View v){
        switch(v.getId()){
            case R.id.bSQLUpdate:
                boolean didItWork = true;
                try{
                    String name = sqlName.getText().toString();
                    String hotness = sqlHotness.getText().toString();

                    HotOrNot entry= new HotOrNot(SqlLiteExample.this);
                    entry.open();
                    entry.createEntry(name, hotness);
                    entry.close();
                }catch(Exception e){
                    didItWork = false;
                    String error = e.toString();
                    Dialog d = new Dialog(this);
                    d.setTitle("Fail");
                    TextView tv=new TextView(this);
                    tv.setText(error);
                    d.setContentView(tv);
                    d.show();
                }finally{
                    if(didItWork){
                        Dialog d = new Dialog(this);
                        d.setTitle("done");
                        TextView tv=new TextView(this);
                        tv.setText("Success");
                        d.setContentView(tv);
                        d.show();
                    }
                }
                break;

            case R.id.bSQLOpenView:
                Intent i= new Intent(this,SQLLiteView.class);
                startActivity(i);
                break;
            case R.id.bgetInfo:
                String s = sqlRow.getText().toString();
                long l = Long.parseLong(s);
                HotOrNot hon = new HotOrNot(this);
                hon.open();
                String returnedName=hon.getName(l);
                String returnedHotness=hon.getHotness(l);
                hon.close();
                sqlName.setText(returnedName);
                sqlHotness.setText(returnedHotness);
                break;
            case R.id.bSQLmodify:
                String mName = sqlName.getText().toString();
                String mHotness = sqlHotness.getText().toString();
                String sRow = sqlRow.getText().toString();
                long lRow = Long.parseLong(sRow);

                HotOrNot ex = new HotOrNot(this);
                ex.open();
                ex.updateEntry(lRow, mName, mHotness);
                ex.close();
                break;

            case R.id.bSQLdelete:
                String sRow1 = sqlRow.getText().toString();
                long lRow1 = Long.parseLong(sRow1);
                HotOrNot ex1 = new HotOrNot(this);
                ex1.open();
                ex1.deleteEntry(lRow1);
                ex1.close();
                break;

        }



    }

}



HotOrNot.java

package myapplication.example1.com.hackonlycom;

/** * Created by AASHISH on 15-04-2017. */
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class HotOrNot extends Activity{
    public static final String KEY_ROWID="id";
    public static final String KEY_NAME="persons_name";
    public static final String KEY_HOTNESS="persons_hotness";
    private static final String DATABASE_NAME="HotOrNotdb";
    private static final String DATABASE_TABLE="peopleTable";
    private static final int DATABASE_VERSION= 2 ;

    private DbHelper ourHelper;
    private  Context ourContext=null;
    private SQLiteDatabase ourDatabase;
    private static class DbHelper extends SQLiteOpenHelper{
        public DbHelper(Context context){
            super(context, DATABASE_NAME, null,DATABASE_VERSION);
        }
        public void onCreate(SQLiteDatabase db)throws SQLException{
            db.execSQL("CREATE TABLE "+ DATABASE_TABLE +" ("+KEY_ROWID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+KEY_NAME+" TEXT NOT NULL, "+KEY_HOTNESS+" TEXT NOT NULL);");
        }
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub            db.execSQL("DROP TABLE IF EXISTS "+DATABASE_TABLE);
            onCreate(db);


        }

    }
    public HotOrNot(){

    }
    public HotOrNot(Context c){
        ourContext = c;
    }
    public HotOrNot open(){
        ourHelper=new DbHelper(ourContext);
        ourDatabase=ourHelper.getWritableDatabase();
        return this;
    }
    public void close(){
        ourHelper.close();

    }
    public long createEntry(String name, String hotness)throws SQLException{
        ContentValues cv = new ContentValues();
        cv.put(KEY_NAME, name);
        cv.put(KEY_HOTNESS, hotness);
        return ourDatabase.insert(DATABASE_TABLE, null, cv);

    }
    public String getData()throws SQLException{
        String[] columns = new String[]{KEY_ROWID, KEY_NAME, KEY_HOTNESS};
        Cursor c= ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);

        String result = "";

        int iRow = c.getColumnIndex(KEY_ROWID);
        int iName = c.getColumnIndex(KEY_NAME);
        int iHotness = c.getColumnIndex(KEY_HOTNESS);
        for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
            result = result + c.getString(iRow)+ " " + c.getString(iName) + " " + c.getString(iHotness)+"\n";

        }
        return result;

    }
    public String getName(long l){
        String[] columns = new String[]{KEY_ROWID, KEY_NAME, KEY_HOTNESS};
        Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_ROWID + " = " + l, null, null, null, null);
        if(c!=null){
            c.moveToFirst();
            String name = c.getString(1);
            return name;
        }


        return null;
    }
    public String getHotness(long l){
        String[] columns = new String[]{KEY_ROWID, KEY_NAME, KEY_HOTNESS};
        Cursor c = ourDatabase.query(DATABASE_TABLE, columns, KEY_ROWID + " = " + l, null, null, null, null);
        if(c!=null){
            c.moveToFirst();
            String hotness=c.getString(2);
            return hotness;
        }
        return null;


    }
    public void updateEntry  (long lRow, String mName, String mHotness) throws SQLException{
        // TODO Auto-generated method stub        ContentValues cvUpdate = new ContentValues();
        cvUpdate.put(KEY_NAME, mName);
        cvUpdate.put(KEY_HOTNESS, mHotness);
        ourDatabase.update(DATABASE_TABLE, cvUpdate, KEY_ROWID + "=" + lRow, null);
    }
    public void deleteEntry(long lRow1) throws SQLException {
        // TODO Auto-generated method stub        ourDatabase.delete(DATABASE_TABLE, KEY_ROWID + "="+lRow1, null);
    }
}


SQLLiteView.java


package myapplication.example1.com.hackonlycom;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

import myapplication.example1.com.hackonlycom.HotOrNot;

public class SQLLiteView extends Activity{
    @Override    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sqlview);
        TextView tv = (TextView)findViewById(R.id.tvSQLinfo);
        HotOrNot info = new HotOrNot(this);
        info.open();
        String data = info.getData();
        info.close();
        tv.setText(data);

    }
}



sqlview.xml

xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:weightSum="1">
    <TextView        android:id="@+id/textView1"        android:layout_width="wrap_content"        android:layout_height="40sp"        android:text="Names"        android:layout_weight="0.15" />
    <TextView        android:id="@+id/textView2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Hotness"        android:layout_weight="0.27" />
    <TextView        android:id="@+id/tvSQLinfo"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_weight="0.34" />
</LinearLayout>


sqlliteexample.xml

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">

    <TextView        android:id="@+id/textView1"        android:layout_width="match_parent"        android:layout_height="30sp"        android:text="Name" />
    <EditText        android:id="@+id/etSQLName"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:ems="10"        android:hint="Enter Name">
        <requestFocus />
    </EditText>
    <TextView        android:id="@+id/textView2"        android:layout_width="wrap_content"        android:layout_height="30sp"        android:text="Hotness Scale" />
    <EditText        android:id="@+id/etSQLHotness"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:ems="10" />
    <Button        android:id="@+id/bSQLUpdate"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Update SQLite Database" />
    <Button        android:id="@+id/bSQLOpenView"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="View" />
    <TextView        android:id="@+id/textView3"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Enter Row ID" />
    <EditText        android:id="@+id/etSQLrawInfo"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:ems="10"        android:inputType="number" />
    <Button        android:id="@+id/bgetInfo"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Get Info" />
    <Button        android:id="@+id/bSQLmodify"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Edit" />
    <Button        android:id="@+id/bSQLdelete"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Delete" />

</LinearLayout>



Reactions:

0 comments:

Post a Comment