title: Update selected column in SQLite table in Android
body: <p>I need to updated selected columns in my database, however i am unable to get the updated data.As soon as the user click on next button, the data should get updated.There are total 12 fields that needs to be updated in the table.</p>
  
  <p>The scenario is bit different, I already have one activity where primarily say if 5 fields are getting inserted, and in 2nd activity I need to UPDATE 5 fields in the SAME TABLE</p>
  
  <p>Thanks in advance</p>
  
  <p>Note: dba is DBAdapter i.e the database and myDB is SQLiteDatabase</p>
  
  <pre><code> nxt = (Button)findViewById(R.id.btn_nxt);
            nxt.setOnClickListener(new OnClickListener(){
  
              public void onClick(View v) {
  
                  String b1=t1.getText().toString();
                  String b2=goal_website_trying_to_achieve_edttxt.getText().toString();
                  String b3=communicate_edttxt.getText().toString();
                  String b4=success_look_like_edttxt.getText().toString();
                  String b5=usp_insights_edttxt.getText().toString();
                  String b6=act_edttxt.getText().toString();
                  String b7=market_edttxt.getText().toString();
                  String b8=your_current_website_www_edttxt.getText().toString();
                  String b9=your_current_website_good_edttxt.getText().toString();
                  String b10=your_current_website_bad_edttxt.getText().toString();
                  String b11=your_current_website_type_edttxt.getText().toString();
                  String b12=your_current_website_update_edttxt.getText().toString();
                  String b13=your_current_website_supporting_edttxt.getText().toString();
  
                  if(!validatescreen2()){
                      return;
                  }
  
                  else{
  
                      myDB = DBAdapter.openDatabase(Newform_Screen2.this,DBAdapter.DATABASE_TABLE);
  
  
  
  
                      String updatequery = "UPDATE"+ DBAdapter.DATABASE_TABLE+ " SET ";       
  
                      updatequery = updatequery + " KEY_MESSAGE_1 = '" + b1 + "',";
                      updatequery = updatequery + " KEY_MESSAGE_2 = '" + b2 + "',";
                      updatequery = updatequery + " KEY_MESSAGE_3 = '" + b3 + "',";
                      updatequery = updatequery + " KEY_MESSAGE_4 = '" + b4 + "',";
                      updatequery = updatequery + " KEY_MESSAGE_5 = '" + b5 + "',";
                      updatequery = updatequery + " KEY_MESSAGE_6 = '" + b6 + "',";
                      updatequery = updatequery + " KEY_YOUR_CURRENT_WEBSITE_1 = '" + b7 + "',";
                      updatequery = updatequery + " KEY_YOUR_CURRENT_WEBSITE_2 = '" + b8 + "',";
                      updatequery = updatequery + " KEY_YOUR_CURRENT_WEBSITE_3 = '" + b9 + "',";
                      updatequery = updatequery + " KEY_YOUR_CURRENT_WEBSITE_TYPE = '" + b10 + "',";
                      updatequery = updatequery + " KEY_YOUR_CURRENT_WEBSITE_NEED = '" + b11 + "',";
                      updatequery = updatequery + " KEY_YOUR_CURRENT_WEBSITE_OTHER = '" + b12 + ",";
  
                      updatequery = updatequery +  "WHERE" + DBAdapter.KEY_ROWID + "=" + mRowId +"';";
  
                      try{
                          myDB.execSQL(updatequery);
                      }catch(Exception e){
                          //Log("Caught while writing response" + e.getLocalizedMessage());
                      }finally{
                          if(myDB != null)myDB.close();
                          showDialog(DIALOG_ID);
                      }
  
                  }
              }
            });
  </code></pre>
  
  <p>and in <code>DBAdapter</code> class</p>
  
  <pre><code>public static SQLiteDatabase openDatabase(Context context, String DBname){
      SQLiteDatabase myDB;
      myDB = context.openOrCreateDatabase(DBname,Context.MODE_PRIVATE, null);
      Cursor rs = null;
      boolean tableExists;
      try{
          rs = myDB.rawQuery("SELECT * FROM " + DATABASE_TABLE + " WHERE 1=0", null );
          tableExists = true;
      }catch(Exception ex){
          tableExists = false;
      }finally{
          if (rs != null) rs.close();
      }
      if(!tableExists) {
          myDB.execSQL(DATABASE_CREATE);
      }
  
      return myDB;
  }
  
  public static void executeQuery(SQLiteDatabase db,String query){
  
      if(db != null){
          db.execSQL(query);
      }
  
  }
  
  public static Cursor selectQuery(SQLiteDatabase db,String query){
  
      if(db != null)
      {
          Cursor c = db.rawQuery(query,null);
          return c;
      }
      else
      return null;
  
  }
  
  public static void closeDatabase(SQLiteDatabase db){
      if (db != null)
      {
          //Log("Closing Database conn");
          db.close();
      }
  }
  </code></pre>
  
  <p>after making changes, but still unable to update..do I need to make any other change??</p>
  
  <pre><code>dba.open();
                          ContentValues args = new ContentValues();
                          args.put(DBAdapter.KEY_MESSAGE_1, b2);
                          args.put(DBAdapter.KEY_MESSAGE_2, b3);
                          args.put(DBAdapter.KEY_MESSAGE_3, b4);
                          args.put(DBAdapter.KEY_MESSAGE_4, b5);
                          args.put(DBAdapter.KEY_MESSAGE_5, b6);
                          args.put(DBAdapter.KEY_MESSAGE_6, b7);
                          args.put(DBAdapter.KEY_YOUR_CURRENT_WEBSITE_1, b8);
                          args.put(DBAdapter.KEY_YOUR_CURRENT_WEBSITE_2, b9);
                          args.put(DBAdapter.KEY_YOUR_CURRENT_WEBSITE_3, b10);
                          args.put(DBAdapter.KEY_YOUR_CURRENT_WEBSITE_TYPE, b11);
                          args.put(DBAdapter.KEY_YOUR_CURRENT_WEBSITE_NEED, b12);
                          args.put(DBAdapter.KEY_YOUR_CURRENT_WEBSITE_OTHER, b13);
  
                          String where = "_id = " + mRowId.toString();
  
                  myDB.update(DBAdapter.DATABASE_TABLE, args,where, null);    
                              dba.close();
                              showDialog(DIALOG_ID);
                      }
  
  
                  }
  
                });
  </code></pre>
  
  <p>after adding try/catch</p>
  
  <pre><code>try{
                          myDB.beginTransaction();
                  dba.open();
                  ContentValues args = new ContentValues();
                  args.put(DBAdapter.KEY_MESSAGE_1, b2);
                  args.put(DBAdapter.KEY_MESSAGE_2, b3);
                  args.put(DBAdapter.KEY_MESSAGE_3, b4);
                  args.put(DBAdapter.KEY_MESSAGE_4, b5);
                  args.put(DBAdapter.KEY_MESSAGE_5, b6);
                  args.put(DBAdapter.KEY_MESSAGE_6, b7);
                  args.put(DBAdapter.KEY_YOUR_CURRENT_WEBSITE_1, b8);
                  args.put(DBAdapter.KEY_YOUR_CURRENT_WEBSITE_2, b9);
                  args.put(DBAdapter.KEY_YOUR_CURRENT_WEBSITE_3, b10);
                  args.put(DBAdapter.KEY_YOUR_CURRENT_WEBSITE_TYPE, b11);
                  args.put(DBAdapter.KEY_YOUR_CURRENT_WEBSITE_NEED, b12);
                  args.put(DBAdapter.KEY_YOUR_CURRENT_WEBSITE_OTHER, b13);
  
                  String where =DBAdapter.KEY_ROWID +"=" + mRowId.toString();
  
  
                  myDB.update(DBAdapter.DATABASE_TABLE, args,where, null);
                  myDB.setTransactionSuccessful();
                      dba.close();
  
                      showDialog(DIALOG_ID);
                      }
                      catch(Exception e){
                          e.printStackTrace();
                      }
                       finally {
                          myDB.endTransaction();
                         }
                  }
  
  
              }
  
            });
  </code></pre>
  
  <p>now i am getting this error</p>
  
  <pre><code>01-29 17:52:38.874: W/System.err(9257): java.lang.NullPointerException
  01-29 17:52:38.874: W/System.err(9257):     at com.example.sales.Newform_Screen2$3.onClick(Newform_Screen2.java:150)
  01-29 17:52:38.874: W/System.err(9257):     at android.view.View.performClick(View.java:4084)
  01-29 17:52:38.883: W/System.err(9257):     at android.view.View$PerformClick.run(View.java:16966)
  01-29 17:52:38.883: W/System.err(9257):     at android.os.Handler.handleCallback(Handler.java:615)
  01-29 17:52:38.883: W/System.err(9257):     at android.os.Handler.dispatchMessage(Handler.java:92)
  01-29 17:52:38.893: W/System.err(9257):     at android.os.Looper.loop(Looper.java:137)
  01-29 17:52:38.893: W/System.err(9257):     at android.app.ActivityThread.main(ActivityThread.java:4745)
  01-29 17:52:38.893: W/System.err(9257):     at java.lang.reflect.Method.invokeNative(Native Method)
  01-29 17:52:38.893: W/System.err(9257):     at java.lang.reflect.Method.invoke(Method.java:511)
  01-29 17:52:38.893: W/System.err(9257):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
  01-29 17:52:38.893: W/System.err(9257):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
  </code></pre>