title: Using multiple tables in one android application
body: <p>My application needs to use multiple tables under the same database. I am developing one global adapter and individual adapter for each tables to implement CRUD. Please see the database Adapter as below:</p>
  
  <pre><code>    import android.content.Context;
      import android.database.SQLException;
      import android.database.sqlite.SQLiteDatabase;
      import android.database.sqlite.SQLiteOpenHelper;
      import android.util.Log;
  
      public abstract class BahiKhataDbAdapter {
  
    protected static final String TAG = "BahiKhataDbAdapter";
        protected DatabaseHelper mDbHelper;
        protected SQLiteDatabase mDb;
  
    private static final String DATABASE_NAME = "bahikhatadata.db";
  
    private static final int DATABASE_VERSION = 1;
  
    protected static final String TABLE_EXPENSE_TYPE =
          "create table expensetype (_id integer primary key autoincrement, "
          + "expense_type text not null);";
  
    protected static final String TABLE_INCOME_TYPE =
          "create table incometype (_id integer primary key autoincrement, "
          + "income_type text not null);";
  
    protected static final String TABLE_GROUP =
          "create table group (_id integer primary key autoincrement, "
          + "group_name text not null);";
  
    protected static final String TABLE_EXPENSE =
          "create table expensetype (_id integer primary key autoincrement, "
          + "expense_amt real not null, note text, expense_type text not null, group   
        text not null, expense_date text not null);";
  
  
    protected static final String TABLE_INCOME =
          "create table expensetype (_id integer primary key autoincrement, "
          + "income_amt real not null, note text, income_type text not null, group 
                text not null, income_date text not null);";
  
        protected final Context mCtx;
  
    protected static class DatabaseHelper extends SQLiteOpenHelper {
  
    DatabaseHelper (Context context){
      super(context, DATABASE_NAME, null, DATABASE_VERSION);
      // TODO Auto-generated constructor stub
    } 
  
  
  @Override
  public void onCreate(SQLiteDatabase db) {
      // TODO Auto-generated method stub
      db.execSQL(TABLE_EXPENSE_TYPE);
      db.execSQL(TABLE_INCOME_TYPE);
      db.execSQL(TABLE_GROUP);
      db.execSQL(TABLE_EXPENSE);
      db.execSQL(TABLE_INCOME);
  }
  
  
  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
      // TODO Auto-generated method stub
      Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
              + newVersion + ", which will destroy all old data");
      db.execSQL("DROP TABLE IF EXISTS expensetype");
      db.execSQL("DROP TABLE IF EXISTS incometype");
      db.execSQL("DROP TABLE IF EXISTS group");
      db.execSQL("DROP TABLE IF EXISTS expense");
      db.execSQL("DROP TABLE IF EXISTS income");       
      onCreate(db);
  }
     }
  
  public BahiKhataDbAdapter(Context ctx) {
      this.mCtx = ctx;
     }
  
  public BahiKhataDbAdapter open() throws SQLException {
      mDbHelper = new DatabaseHelper(mCtx);
      mDb = mDbHelper.getWritableDatabase();
      return this;
  }
  
  public void close() {
      mDbHelper.close();
     }
    }
  </code></pre>
  
  <p>and adapter for expensetype is as below:</p>
  
  <pre><code>     import android.content.ContentValues;
       import android.content.Context;
       import android.database.Cursor;
       import android.database.DatabaseUtils;
  
       public class ExpenseTypeDbAdapter extends BahiKhataDbAdapter {
  
  // Database fields
          public static final String KEY_ROWID = "_id";
          public static final String KEY_NAME = "expense_type";
          private static final String DB_TABLE = "expensetype";
  
  public ExpenseTypeDbAdapter(Context context) {
      // TODO Auto-generated constructor stub
      super(context);
  }
  
  
  /**
   * Create a new business If the business is successfully created return the new
   * rowId for that note, otherwise return a -1 to indicate failure.
   */
  public long createExpenseType(String name) {
      //ContentValues values = createContentValues(name);
  
      ContentValues args = new ContentValues();
      args.put(KEY_NAME, name);
      return mDb.insert(DB_TABLE, null, args);
  }
  
  /*private ContentValues createContentValues(String expense_type) {
      ContentValues values = new ContentValues();
      values.put(KEY_NAME, expense_type);         
      return values;
  }*/
  
  /**
   * Deletes expensetype record
   */
  public boolean deleteExpenseType(String expense_type) {
      return mDb.delete(DB_TABLE, "KEY_NAME=?", new String[] {expense_type}) &gt; 0;
  }
  
  /**
   * Return a Cursor over the list of all todo in the database
   * 
   * @return Cursor over all notes
   */
  public Cursor fetchAllExpenseType() {
      return mDb.query(DB_TABLE, new String[] { KEY_ROWID, KEY_NAME,}, null, null, null, null, null);
  }
  
  public long count() {
         return DatabaseUtils.queryNumEntries(mDb, DB_TABLE);
    }
  
     }
  </code></pre>
  
  <p>and using the ExpenseTypeDbAdapter in one of my activity as below: (in this activity I am using the Spinner to populate the data of expensetype table. Please see the code as below:</p>
  
  <pre><code>   import android.app.Activity;
    //import android.app.AlertDialog;
     import android.content.Intent;
     import android.database.Cursor;
     import android.net.Uri;
     import android.os.Bundle;
     import android.view.View;
     import android.view.View.OnClickListener;
     import android.widget.AdapterView;
     import android.widget.AdapterView.OnItemSelectedListener;
     import android.widget.Button;
     import android.widget.SimpleCursorAdapter;
     import android.widget.Spinner;
     import android.widget.Toast;
  
     public class ExpenseTypeList extends Activity  implements OnClickListener ,    
         OnItemSelectedListener{
  
  /** Properties **/
    protected Spinner expenseSpinner;
    protected Button expenseDone;
    protected Button addNewExpenseType;
    protected ExpenseTypeDbAdapter dbHelper;
    protected String expenseType;
  
    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.expense);
  
      dbHelper = new ExpenseTypeDbAdapter(this);
  
      //configure the widgets
      addNewExpenseType = (Button) findViewById(R.id.add_new_expense_type);
      expenseDone       = (Button) findViewById(R.id.expense_done);
  
      // Setup ClickListeners
      addNewExpenseType.setOnClickListener(this);
      expenseDone.setOnClickListener(this);
  
      // initialize the database
      dbHelper.open();
  
      expenseSpinner = (Spinner) findViewById(R.id.expense_spinner);
  
      //populated the spinner from the ExpenseData db
  
      if(dbHelper.count() == 0) {
            dbHelper.createExpenseType("Jan");
            dbHelper.createExpenseType("Feb");
            dbHelper.createExpenseType("Mar");
            dbHelper.createExpenseType("Apr");
  
          }
  
      getSpinnerData();
      expenseSpinner.setOnItemSelectedListener(this);
  
    }
  
      @Override
      public void onItemSelected(AdapterView&lt;?&gt; spinner, View view, int position,
              long id) {
          // TODO Auto-generated method stub
          if(spinner == expenseSpinner) {
                // get the expense type
              Cursor cursor = (Cursor) spinner.getSelectedItem();
              expenseType   = cursor.getString(1);
            }
      }
  
      @Override
      public void onNothingSelected(AdapterView&lt;?&gt; arg0) {
          // TODO Auto-generated method stub
  
      }
  
      @Override
      public void onClick(View v) {
          // TODO Auto-generated method stub
          if(v == expenseDone) {
              //Toast.makeText(this, expenseType + " selected", Toast.LENGTH_LONG).show();
              Uri data = Uri.parse(expenseType);
              Intent result = new Intent(null, data);
              result.putExtra("EXPENSE_TYPE", expenseType);
              setResult(RESULT_OK, result);
              finish();
  
          }
          if(v == addNewExpenseType){
              Toast.makeText(this, expenseType + " selected", Toast.LENGTH_LONG).show();
              Intent intent = new Intent(this, AddExpenseType.class);
              startActivity(intent);
             }
      }
  
      public void getSpinnerData(){
  
          Cursor cursor = dbHelper.fetchAllExpenseType();
  
          String[] from = new String[] {ExpenseTypeDbAdapter.KEY_NAME}; //use class name directly
  
          int[] to = new int[] {android.R.id.text1};
  
          SimpleCursorAdapter expenseCursorAdapter = new SimpleCursorAdapter(
            this,
            android.R.layout.simple_spinner_item,
            cursor,
            from,
            to
          );
  
          expenseSpinner.setAdapter(expenseCursorAdapter);
          expenseCursorAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);        
      }
  
      @Override
      protected void onDestroy() {
          super.onDestroy();
          if (dbHelper != null) {
              dbHelper.close();
          }
      }
        }
  </code></pre>
  
  <p>ExpenseTypeList is not working; it is giving error of SQLite = 1; syntax error in create of group table. I tired to run after commenting the "create group" syntax and uninstalled the application from Android emulator; but still getting the error. </p>
  
  <p>Could somebody help me out to run this. Thanks in advance. </p>