Social

Wednesday, January 1, 2014

SQLite Android JAVA Tutorial 04 – Update DB & Delete from DB



Update DB :

Following Java code shows how we can use UPDATE statement to update any record from our table test_table,

public class Sqlite_Tute extends Activity {
      
       SQLiteDatabase db;

       @Override
       protected void onCreate(Bundle savedInstanceState) {
              // TODO Auto-generated method stub
              super.onCreate(savedInstanceState);
              requestWindowFeature(Window.FEATURE_NO_TITLE);
              setContentView(R.layout.sqlite_tute);
             
             
              try
              {
                     db = openOrCreateDatabase("testDB", MODE_PRIVATE,null);
                     db.execSQL("CREATE TABLE IF NOT EXISTS test_table (_id INTEGER PRIMARY KEY AUTOINCREMENT,_name TEXT,_email TEXT,_age INTEGER);");
              }
              catch(Exception e)
              {
                     Log.d("rockman", "DB Creation or TABLE Creation ERROR--"+e.toString());
              }

              /////////////////////////// UPDATE PART

String email = ”test123@test.com”; //new value
int age = 34; //new value

              try
              {
                     ContentValues values = new ContentValues();

                     values.put("_email", email);
                     values.put("_age", age);
      
                     db.update("test_table", values, "id=15", null);
}
              catch(Exception e)
              {
                     Log.d("rockman", "UPDATE ERROR--"+e.toString());
              }

       }
}


NOTE – It is same as SELECT operation, different is in update function and it arguments. 3rd argument represent the normal mysql WHERE clause. So here it only updated the values related to id=15.


Delete from DB :

Following Java code shows how we can use DELETE statement to delete any record from our table test_table,

public class Sqlite_Tute extends Activity {
      
       SQLiteDatabase db;

       @Override
       protected void onCreate(Bundle savedInstanceState) {
              // TODO Auto-generated method stub
              super.onCreate(savedInstanceState);
              requestWindowFeature(Window.FEATURE_NO_TITLE);
              setContentView(R.layout.sqlite_tute);
             
             
              try
              {
                     db = openOrCreateDatabase("testDB", MODE_PRIVATE,null);
                     db.execSQL("CREATE TABLE IF NOT EXISTS test_table (_id INTEGER PRIMARY KEY AUTOINCREMENT,_name TEXT,_email TEXT,_age INTEGER);");
              }
              catch(Exception e)
              {
                     Log.d("rockman", "DB Creation or TABLE Creation ERROR--"+e.toString());
              }

              /////////////////////////// DELETE PART


              try
              {
                     db.rawQuery("DELETE FROM test_table WHERE id=15", null);     
}
              catch(Exception e)
              {
                     Log.d("rockman", "DELETE ERROR--"+e.toString());
              }

       }
}

NOTE – It’s very easy to delete. Just needed to write the current mysql command to delete.



Conclusion :

So I hope these series of tutorials help you to get some basic knowledge regarding how the use of SQLite with android JAVA. So if have any doubts please feel free to ask. \m/



No comments:

Post a Comment

 

Search This Blog

Most Reading

Protected by Copyscape Duplicate Content Software

Facebook Page