Friday 12 May 2017

SQLite Database



                                                              SQLite DataBase


SQLite is a opensource SQL database that stores data to a text file on a device. Android comes in with built in SQLite database implementation.
  • SQLite supports all the relational database features. In order to access this database, you don't need to establish any kind of connections for it like JDBC,ODBC e.t.c
  • We have 2 types of methods in SQLiteDatabase getWriteableDatabase() and getReadableDatabase().
  • Using getWriteableDatabase() we can able write, read and modify the data to table.
  • Using getReadableDatabase() we can able do only read we can't able to insert or update. 
  • We have 2 types  of Classes. Using this two classes we can able to create new  Database and table and we can do insert, update and delete the date. 
   
  1.SQLite OpenHelper

  2.SQLite DataBase


SQLiteOpenHelper:

Using this Helper class we can able to create DataBase and we can Upgrade and downgrade the DataBase.

onCreate();
onUpgrade();
onDowngrade();

SQLiteDataBase:

Using this class we can able to create new table and we can insert data to the table and we can delete the data and we can update the data.

Syntax for create table:

public void onCreate(SQLiteDatabase db) {

    db.execSQL("create table employee(_id integer primary key, name text, 
phone text)"); 
}

Syntax for insert table:

For insert data we need to use ContentValues Class.

ContentValues is a name value pair, used to insert or update values into database tables.

ContentValues cv=new ContentValues();
 cv.put("name","Android");
 cv.put("phone","123455678");
 db.insert("employee",null, cv);


If you want to read the data from DataBase you can use query() or rawQuery() methods.
which will return cursor.

  Cursor is a temporary buffer area which stores results from a SQLiteDataBase query.

 Cursor cursor= db.query("employee",null, null,null,null,null,null);
    while(cursor.moveToNext()){
        Toast.makeText(MainActivity.this,cursor.getString(1)+""
         +cursor.getString(2),Toast.LENGTH_LONG).show();
    }
}

Example Code:



MainActivity:


public class MainActivity extends Activity {
    private EditText mNameEt;
    private EditText mPhoneEt;
    private MyOpenHelper mMyOpenHelper;
    private SQLiteDatabase mSqlDb;
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mNameEt=(EditText)findViewById(R.id.name_et);
        mPhoneEt=(EditText)findViewById(R.id.phone_et);
        mMyOpenHelper = new MyOpenHelper(MainActivity.this,"EMPDB",null , 1);
        mSqlDb = mMyOpenHelper.getWritableDatabase();
    }
    public void insertData(View view) {
        ContentValues cv=new ContentValues();
        cv.put("name",mNameEt.getText().toString());
        cv.put("phone",mPhoneEt.getText().toString());
       long id = mSqlDb.insert("employee",null, cv);
        Toast.makeText(MainActivity.this, String.valueOf(id), 
        Toast.LENGTH_LONG).show();
    }
    public void readData(View view) {
        Cursor cursor= mSqlDb.query("employee",null, null,null,null,null,null);
        while(cursor.moveToNext()){
            Toast.makeText(MainActivity.this,cursor.getString(1)+""
           + cursor.getString(2),Toast.LENGTH_LONG).show();
        }
    }
}
OpenHelper Class:
public class MyOpenHelper extends SQLiteOpenHelper{
    public MyOpenHelper(Context context, String name, 
SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }
    @Override    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table employee(_id integer primary key,
 name text, phone text)");
    }
    @Override    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }

    @Override    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        super.onDowngrade(db, oldVersion, newVersion);
    }
}










0 comments:

Post a Comment