title: Which way is better accesing android sqlitehelper object from multiple locations?
body: <p>Assume i will store Students, Employees, Teachers etc.</p>
  
  <p>Should i create 3 tables in <strong>one</strong> database or should i create a database for each above object.</p>
  
  <ul>
  <li><p>If i choose to use the way one, i need to use one instance of
  sqlitehelper in all parts of my app. So  my classes which dies
  insert, update, delete operations must access that object all the
  time so a little bit extra coding which should be fine.</p></li>
  <li><p>If second way, then i don't need to do anything, just create separate
  classes and not having to worry about anything. Or is it? Is creating
  databases for one table or similar conditions good practice? Is it<br>
  bad for phone performance</p></li>
  </ul>
  
  <p>?</p>
  
  <p>What do you suggest for that?</p>
  
  <pre><code>public class StudentDbAdapter{
  
  StudentHelper sh;
  
  public StudentDbAdapter(Context c)
  {
      sh = new StudentHelper(c);
  }
  
  public Student select(long id)
  {
      Student s = new Student();
      SQLiteDatabase db = sh.getWritableDatabase();
      String query = StudentHelper.ID+" = ?";
      Cursor c = db.query(StudentHelper.TABLE_NAME,null,query,new String[]{Long.toString(id)},null,null,null);
      if(c.moveToFirst()) {
          s.setId(c.getLong(c.getColumnIndex(StudentHelper.ID)));
          s.setFirstName(c.getString(c.getColumnIndex(StudentHelper.FIRSTNAME)));
          s.setLastName(c.getString(c.getColumnIndex(StudentHelper.LASTNAME)));
          s.setGpa(c.getFloat(c.getColumnIndex(StudentHelper.GPA)));
      }
      c.close();
      return s;
  }
  
  public ArrayList&lt;Student&gt; selectSome(int count)
  {
      ArrayList&lt;Student&gt; stus = new ArrayList&lt;&gt;();
      String sql = "SELECT * FROM "+StudentHelper.TABLE_NAME+" LIMIT ?";
      SQLiteDatabase db = sh.getWritableDatabase();
  
      Cursor c = db.rawQuery(sql,new String[]{Integer.toString(count)});
      if(c.getCount()&gt;0) {
          c.moveToFirst();
          do {
              Student s = new Student();
              s.setId(c.getLong(c.getColumnIndex(StudentHelper.ID)));
              s.setFirstName(c.getString(c.getColumnIndex(StudentHelper.FIRSTNAME)));
              s.setLastName(c.getString(c.getColumnIndex(StudentHelper.LASTNAME)));
              s.setGpa(c.getFloat(c.getColumnIndex(StudentHelper.GPA)));
              stus.add(s);
          } while (c.moveToNext());
      }
      c.close();
  
      return stus;
  }
  
  public ArrayList&lt;Student&gt;  selectAll()
  {
      ArrayList&lt;Student&gt; stus = new ArrayList&lt;&gt;();
      SQLiteDatabase db = sh.getWritableDatabase();
      Cursor c =db.query(StudentHelper.TABLE_NAME,null,null, null,null,null,null);
      if(c.getCount()&gt;0) {
          c.moveToFirst();
          do {
              Student s = new Student();
              s.setId(c.getLong(c.getColumnIndex(StudentHelper.ID)));
              s.setFirstName(c.getString(c.getColumnIndex(StudentHelper.FIRSTNAME)));
              s.setLastName(c.getString(c.getColumnIndex(StudentHelper.LASTNAME)));
              s.setGpa(c.getFloat(c.getColumnIndex(StudentHelper.GPA)));
              stus.add(s);
          } while (c.moveToNext());
      }
      c.close();
      return stus;
  }
  public long insert(Student s)
  {
      String sql = "INSERT INTO "+StudentHelper.TABLE_NAME+"" +
              "("+StudentHelper.FIRSTNAME+","+StudentHelper.LASTNAME+"," +
              " "+StudentHelper.GPA+") values(?,?,?)";
      SQLiteDatabase db = sh.getWritableDatabase();
      db.beginTransaction();
      SQLiteStatement stmt = db.compileStatement(sql);
  
      stmt.bindString(1,s.getFirstName());
      stmt.bindString(2,s.getLastName());
      stmt.bindDouble(3,s.getGpa());
  
      s.setId(stmt.executeInsert());
      stmt.clearBindings();
  
      db.setTransactionSuccessful();
      db.endTransaction();
  
      return s.getId();
  }
  public ArrayList&lt;Long&gt; insertMany(ArrayList&lt;Student&gt; stus)
  {
      ArrayList&lt;Long&gt; ids = new ArrayList&lt;&gt;();
      String sql = "INSERT INTO "+StudentHelper.TABLE_NAME+"" +
              "("+StudentHelper.FIRSTNAME+","+StudentHelper.LASTNAME+"," +
              " "+StudentHelper.GPA+") values(?,?,?)";
      SQLiteDatabase db = sh.getWritableDatabase();
      db.beginTransaction();
      SQLiteStatement stmt = db.compileStatement(sql);
  
      for(Student s:stus) {
  
          stmt.bindString(1, s.getFirstName());
          stmt.bindString(2, s.getLastName());
          stmt.bindDouble(3, s.getGpa());
  
          s.setId(stmt.executeInsert());
          ids.add(s.getId());
          stmt.clearBindings();
      }
  
      db.setTransactionSuccessful();
      db.endTransaction();
  
      return ids;
  }
  public int update(long id,int columnIndex, String value)
  {
      String sql = "UPDATE "+StudentHelper.TABLE_NAME+" " +
              "SET "+StudentHelper.COLUMN_NAMES[columnIndex]+" =?  WHERE "+StudentHelper.ID+" = ?";
      SQLiteDatabase db = sh.getWritableDatabase();
      db.beginTransaction();
  
      SQLiteStatement stmt = db.compileStatement(sql);
      stmt.bindDouble(1,Double.parseDouble(value));
      stmt.bindLong(2,id);
  
      int result = stmt.executeUpdateDelete();
      db.setTransactionSuccessful();
      db.endTransaction();
  
      return result;
  }
  
  public int delete(long id)
  {
      String sql = "DELETE FROM "+StudentHelper.TABLE_NAME+" WHERE "+StudentHelper.ID+" = ?";
      SQLiteDatabase db = sh.getWritableDatabase();
      db.beginTransaction();
  
      SQLiteStatement stmt = db.compileStatement(sql);
      stmt.bindLong(1,id);
  
      int result = stmt.executeUpdateDelete();
      db.setTransactionSuccessful();
      db.endTransaction();
  
      return result;
  }
  public int deleteAll()
  {
      String sql = "DELETE FROM "+StudentHelper.TABLE_NAME+"";
      SQLiteDatabase db = sh.getWritableDatabase();
      db.beginTransaction();
  
      SQLiteStatement stmt = db.compileStatement(sql);
      int result = stmt.executeUpdateDelete();
  
      db.setTransactionSuccessful();
      db.endTransaction();
  
      return result;
  }
  
  
  static class StudentHelper extends SQLiteOpenHelper {
  
      private static final String DATABASE_NAME = "HumanInfo";
      private static final int DATABASE_VERSION = 3;
      private Context c;
  
      /*****************TABLE 1********************/
      private static final String TABLE_NAME = "Student";
      private static final String ID = "_id";
      private static final String FIRSTNAME ="Firstname";
      private static final String LASTNAME ="Lastname";
      private static final String GPA ="GPA";
      private static final String CREATE_TABLE = "CREATE TABLE "+ TABLE_NAME +"(" +
              ""+ID+" INTEGER PRIMARY KEY AUTOINCREMENT," +
              ""+FIRSTNAME+" VARCHAR(50) NOT NULL," +
              ""+LASTNAME+" VARCHAR(50) NOT NULL," +
              ""+GPA+" REAL NOT NULL CHECK("+GPA+"&lt; 4)" +
              ");";
      private static final String DROP_TABLE = "DROP TABLE IF EXISTS "+ TABLE_NAME +"";
      private static final String[] COLUMN_NAMES = new String[]{ID,FIRSTNAME,LASTNAME,GPA};
      /*****************TABLE 1********************/
  
  
      public StudentHelper(Context c)
      {
          super(c,DATABASE_NAME,null,DATABASE_VERSION);
          this.c = c;
      }
      @Override
      public void onCreate(SQLiteDatabase sqLiteDatabase) {
          try {
              sqLiteDatabase.execSQL(CREATE_TABLE);
          } catch (SQLException e) {
              Toast.makeText(c, e.toString(), Toast.LENGTH_SHORT).show();
          }
      }
  
      @Override
      public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i2) {
          try {
              sqLiteDatabase.execSQL(DROP_TABLE);
              onCreate(sqLiteDatabase);
          } catch (SQLException e) {
              Toast.makeText(c,e.toString(),Toast.LENGTH_SHORT).show();
          }
      }
  }
  </code></pre>
  
  <p>}</p>