title: Inserting and updating data on multiple table on SQLite database
body: <p>In my project, I created a SQLite database with using a class that extends <code>SQLiteOpenHelper</code> library in android to creating three tables in one database.
  the problem is when in <code>MainActivity</code> class (comes below) I want to create and update a data row, instead of putting data in desired table, <strong>they saved only in "table3db" table and the other tables are still empty</strong> (I saw this condition with an application can browse SQLite databases), but I want to save each data in desired table. for example first and second data must be saved in first table and third must be in second table and fourth data integer must be save in third table.</p>
  
  <p><img src="https://i.stack.imgur.com/Othad.png" alt="table"></p>
  
  <p>what should I do to correct this problem??</p>
  
  <p>for first step I created three Tables with below codes in <code>DatabaseHelper</code>:</p>
  
  <pre><code>public class DatabaseHelper extends SQLiteOpenHelper {
  
      private final String TAG = "DatabaseHelper";
      private static final String DATABASE_NAME = "db";    
      private static final int DATABASE_VERSION = 1;
  
      private static final String COLUMN_ID      = "_id";
      private static final String COLUMN_NAME    = "name";
      private static final String COLUMN_VALUE   = "value";
      private static final String COLUMN_VALUE2 = "value2";
      private static final String TABLE_NAME     = "table1db";
      private static final String CREATE_TABLE   = "CREATE TABLE " + TABLE_NAME + " (" + 
          COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
          COLUMN_NAME + " INTEGER," +
          COLUMN_VALUE + " INTEGER," +
          COLUMN_VALUE2 + " TEXT" +
          ");";
  
      private static final String COLUMN_ID_2    = "_id";
      private static final String COLUMN_NAME_2  = "name";
      private static final String COLUMN_VALUE_2 = "value";
      private static final String COLUMN_VALUE2_2 = "value2";
      private static final String TABLE_NAME_2   = "table2db";
      private static final String CREATE_TABLE_2   = "CREATE TABLE " + TABLE_NAME_2 + " (" + 
              COLUMN_ID_2 + " INTEGER PRIMARY KEY AUTOINCREMENT," +
              COLUMN_NAME_2 + " INTEGER," +
              COLUMN_VALUE_2 + " INTEGER," +
              COLUMN_VALUE2_2 + " TEXT" +
              ");";
  
      private static final String COLUMN_ID_3    = "_id";
      private static final String COLUMN_NAME_3  = "name";
      private static final String COLUMN_VALUE_3 = "value";
      private static final String COLUMN_VALUE2_3 = "value2";
      private static final String TABLE_NAME_3   = "table3db";
      private static final String CREATE_TABLE_3   = "CREATE TABLE " + TABLE_NAME_3 + " (" + 
              COLUMN_ID_3 + " INTEGER PRIMARY KEY AUTOINCREMENT," +
              COLUMN_NAME_3 + " INTEGER," +
              COLUMN_VALUE_3 + " INTEGER," +
              COLUMN_VALUE2_3 + " TEXT" +
              ");";
  
      @Override
      public void onCreate(SQLiteDatabase db) {
          db.execSQL(CREATE_TABLE_2);
          Log.i(TAG, "editTexts Table created.");
  
          db.execSQL(CREATE_TABLE);
          Log.i(TAG, "Table created.");
  
          db.execSQL(CREATE_TABLE_3);
          Log.i(TAG, "Table created.");
      }
  
      public DatabaseHelper(Context context) {
          super(context, DATABASE_NAME, null, DATABASE_VERSION);
          Log.i(TAG, "Object created.");
      }
  
      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
          newVersion=oldVersion+1;
          Log.w(TAG, "Upgrading database from version " + oldVersion 
                  + " to "
                  + newVersion + ", which will destroy all old data");
          db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME + ";");
          db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME_2 + ";");
          db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME_3 + ";");
  
          onCreate(db);
      }
  
      public String getTableName(int tableNumber) {
          String out="";
          switch (tableNumber){
          case 1:
              out=TABLE_NAME;
          case 2:
              out=TABLE_NAME_2;
          case 3:
              out=TABLE_NAME_3;
          }
          return out;
      }
  
      public String getRowIdName(int tableNumber) {
          String out="";
          switch (tableNumber){
          case 1:
              out=COLUMN_ID;
          case 2:
              out=COLUMN_ID_2;
          case 3:
              out=COLUMN_ID_3;
          }
          return out;
      }
  }
  </code></pre>
  
  <p>Then I created this class to use DatabaseHelper class with following code by the name of <code>DatabaseHandler</code></p>
  
  <pre><code>public class DatabaseHandler {
  
      private final String TAG      = "DatabaseHandler";
      static final  String NAME    = "name";
      static final  String VALUE   = "value";
      static final  String VALUE2  = "value2";
      private DatabaseHelper dbHelper;
      private SQLiteDatabase database;
  
      public DatabaseHandler(Context context) {
          dbHelper = new DatabaseHelper(context);
          Log.i(TAG, "DatabaseHelper Object created.");
      }
  
      public void open() throws SQLException {
          database = dbHelper.getWritableDatabase();
      }
  
      public void close() {
          dbHelper.close();
      }
  
      public void insertCBox(int tableNumber, CBox checkBox) {
          ContentValues cv = new ContentValues();
  
          cv.put(NAME,  checkBox.getName());
          cv.put(VALUE, checkBox.getStatus());
          cv.put(VALUE2, checkBox.getText());
          database.insert(dbHelper.getTableName(tableNumber), NAME, cv);
  
          Log.i(TAG, "Contact added successfully.");
      }
  
      public void deleteCheckBox(int tableNumber, int id) {
          database.delete(dbHelper.getTableName(tableNumber), dbHelper.getRowIdName(tableNumber) + "=" + id, null);
      }
  
      public void updateCheckBox(int tableNumber, int id,int name,int state, String text) {
  
          ContentValues cv = new ContentValues();
          cv.put(NAME,  name);
          cv.put(VALUE, state);
          cv.put(VALUE2, text);
          database.update(dbHelper.getTableName(tableNumber), cv, dbHelper.getRowIdName(tableNumber) + "=" + id, null);
      }
  
      public CBox getCBox(int tableNumber, int id){
          Log.i(TAG, "getCBOX started");
          Cursor cursor = database.query(dbHelper.getTableName(tableNumber), null, null, null, null, null, null);
          Log.i(TAG, "cursor query done");
          cursor.moveToFirst();
          cursor.moveToPosition(id-1);
          Log.i(TAG, "cursor is here: "+ cursor.getPosition());
  //        cursor.moveToPosition(id--);
          Log.i(TAG, "cursor moved to position successfully "+ id--);
          CBox CBox = cursorToContact(cursor);
          Log.i(TAG, "cursor to contact done");
          cursor.close();
          Log.i(TAG, "cursor closed");
          return CBox;
      }
  
  
      public void clearTable(int tableNumber) {
          database.delete(dbHelper.getTableName(tableNumber), null, null);
      }
  
      private CBox cursorToContact(Cursor cursor) {
          CBox checkBox = new CBox();
          Log.i(TAG, "cursor to contact &gt; started");
          checkBox.setId(cursor.getInt(0));
          Log.i(TAG, "cursor to contact &gt; getInt(0) done " + checkBox.getId());
          checkBox.setName(cursor.getInt(1));
          Log.i(TAG, "cursor to contact &gt; getInt(1) done " + checkBox.getName());
          checkBox.setStatus(cursor.getInt(2));
          Log.i(TAG, "cursor to contact &gt; getInt(2) done " + checkBox.getStatus());
          checkBox.setText(cursor.getString(3));
          Log.i(TAG, "cursor to contact &gt; getString(3) done " + checkBox.getText());
  
          return checkBox;
      }
  
  }
  </code></pre>
  
  <p>for 3rd step in my <code>Mainactivity</code> class I used following codes to use database and inserting and updating and saving data:</p>
  
  <pre><code>public class MainActivity extends Activity {
  
      private DatabaseHandler dbHandler;
      private static final int databaseTableNumber1=1;
      private static final int databaseTableNumber2=2;
      private static final int databaseTableNumber3=3;
  
      private CBox cBox01;
      private CBox cBox02;
      private CBox cBox03;
      private CBox cBox04;
  
      private boolean firstRunPassed=false;
      private SharedPreferences sharedperefs;
         private String preferenceName = "Preferences";
  
      @Override
      public void onCreate(Bundle icicle) {
          super.onCreate(icicle);
          Log.i(TAG, "On Create");
          setContentView(R.layout.activity_main);
  
          dbHandler = new DatabaseHandler(this);
  
          final Button saveButton = (Button) findViewById(R.id.saveButton);         
          saveButton.setOnClickListener(new OnClickListener() {
              @Override
              public void onClick(View v) {
                  dbHandler.open();
                  int state;
                  String text;
                  CheckBox checkBox01= (CheckBox) findViewById(R.id.checkBox1);
                  if(checkBox01.isChecked()) state=1; else state=0;
                  dbHandler.updateCheckBox(databaseTableNumber1,1,R.id.checkBox1,state,"");
  
                  RadioGroup radioGroup01=(RadioGroup) findViewById(R.id.radioGroup1);
                  state= radioGroup01.getCheckedRadioButtonId();
                  dbHandler.updateCheckBox(databaseTableNumber1,2, R.id.radioGroup1, state,"");
  
                  EditText editText01=(EditText) findViewById(R.id.editText1);
                  text=editText01.getText().toString();
                  dbHandler.updateCheckBox(databaseTableNumber2,1, R.id.editText1,state,text);
  
                  ToggleButton toggleButton01 =(ToggleButton) findViewById(R.id.toggleButton1);
                  if(toggleButton01.isChecked()) state=1; else state=0;
                  dbHandler.updateCheckBox(databaseTableNumber3,1,R.id.toggleButton1,state,"");
  
                  dbHandler.close();
              }
          });
      }
  
      @Override
      protected void onPause(){
          super.onPause();
          Log.i(TAG, "On Pause");
          sharedperefs = getSharedPreferences(preferenceName, MODE_PRIVATE);
          SharedPreferences.Editor editor =sharedperefs.edit();
          firstRunPassed=true;
          editor.putBoolean("firstRunPassed", firstRunPassed);
          editor.commit();    
      }
  
      @Override
      protected void onResume() {
          super.onResume();
          Log.i(TAG, "On Resume");
  
          sharedperefs=getSharedPreferences(preferenceName, MODE_PRIVATE);
          firstRunPassed=sharedperefs.getBoolean("firstRunPassed", false);
  
          dbHandler.open();
          Log.i(TAG, "dbhandler opened");
  
          if(firstRunPassed){
              cBox01=new CBox(); 
              cBox01=dbHandler.getCBox(databaseTableNumber1,1);
              CheckBox checkBox01= (CheckBox) findViewById(R.id.checkBox1);
              if(cBox01.getStatus()==1) 
                  checkBox01.setChecked(true); 
              else 
                  checkBox01.setChecked(false);    
  
              cBox02=new CBox();
              cBox02=dbHandler.getCBox(databaseTableNumber1,2);
              RadioGroup radioGroup01=(RadioGroup) findViewById(R.id.radioGroup1);
              radioGroup01.check(cBox02.getStatus());
  
              cBox03=new CBox();
              cBox03=dbHandler.getCBox(databaseTableNumber2,4);
              EditText editText01=(EditText) findViewById(R.id.editText1);
              editText01.setText(cBox03.getText());
  
              cBox04=new CBox();
              cBox04=dbHandler.getCBox(databaseTableNumber3,1);
              ToggleButton toggleButton01 =(ToggleButton) findViewById(R.id.toggleButton1);
              if(cBox04.getStatus()==1) 
                  toggleButton01.setChecked(true); 
              else 
                  toggleButton01.setChecked(false);
          } else {
              cBox01 = new CBox();  cBox01.setId(1); cBox01.setName(R.id.checkBox1); cBox01.setStatus(0); cBox01.setText(""); dbHandler.insertCBox(databaseTableNumber1,cBox01);
              cBox02 = new CBox();  cBox02.setId(2); cBox02.setName(R.id.radioGroup1); cBox02.setStatus(0); cBox02.setText(""); dbHandler.insertCBox(databaseTableNumber1,cBox02);
              cBox03 = new CBox();  cBox03.setId(1); cBox03.setName(R.id.editText1); cBox03.setStatus(0); cBox03.setText("Start please"); dbHandler.insertCBox(databaseTableNumber2,cBox03);
              cBox04 = new CBox();  cBox04.setId(1); cBox04.setName(R.id.toggleButton1); cBox04.setStatus(0); cBox04.setText(""); dbHandler.insertCBox(databaseTableNumber3,cBox04);
          }
          dbHandler.close();
          Log.i(TAG, "dbhandler closed");
      }
  }
  </code></pre>
  
  <p>and the <code>CBox</code> is my last class, used for setting and getting data cells:</p>
  
  <pre><code>public class CBox {
  
      private int id;
      private int name;
      private int Status;
      private String text;
      private String unit;
  
      public long getId() {
          return id;
      }
  
      public String getIdInString() {
          return Long.toString(id);
      }
  
      public int getName() {
          return name;
      }
  
      public int getStatus() {
          return Status;
      }
  
      public String getText() {
          return text;
      }
  
      public String getUnit() {
          return unit;
      }
  
      public void setId(int id) {
          this.id = id;
      }
  
      public void setName(int name) {
          this.name = name;
      }
  
      public void setStatus(int status) {
          this.Status = status;
      }
  
      public void setText(String text) {
          this.text = text;
      }
  
      public void setUnit(String unit) {
          this.unit = unit;
      }
  }
  </code></pre>