title: Custom table creation
body: <p>I have made a database using dbhelper previously, and now I want to add a table in that using this code. But I am getting error that no such table found. Please can you help me out with this.</p>
  
  <p>Should I need to open database every time and can I make two tables in using a single <code>dbHelper</code> class for two different activities.</p>
  
  <pre><code>package com.example.kunj.scope;
  
  import android.content.ContentValues;
  import android.content.Context;
  import android.database.sqlite.SQLiteDatabase;
  import android.database.sqlite.SQLiteOpenHelper;
  import android.os.Bundle;
  import android.util.Log;
  import android.widget.Toast;
  
  import java.util.ArrayList;
  import java.util.List;
  
  import static android.database.sqlite.SQLiteDatabase.openDatabase;
  import static com.example.kunj.scope.ScopeDBHelper.CREATE_TABLE;
  import static com.example.kunj.scope.ScopeDBHelper.DATABASE_NAME;
  import static com.example.kunj.scope.ScopeDBHelper.DATABASE_VERSION;
  
  
  /**
   * Created by kunj on 2/8/2018.
   */
  
  public class CustomScopeDBHelper {
  
  
      public Context context;
      SQLiteDatabase db;
      ScopeOpenHelper scopeOpenHelper;
      String DBPATH;
      public CustomScopeDBHelper(Context context) {
          this.context = context;
          scopeOpenHelper = new ScopeOpenHelper(context);
         // this.DBPATH = this.context.getDatabasePath(DATABASE_NAME).getAbsolutePath();
        //  Log.e("Path 1", DBPATH);
          db = scopeOpenHelper.getWritableDatabase();
      }
  
  
      public static String query;
      public static final String DATABASE_NAME = "scope.db";
      public static final int DATABASE_VERSION = 1;
      String TABLE_NAME;
  
    public void getQuery(List&lt;String&gt; columnName, String tableName) {
          TABLE_NAME = tableName;
          /*int i = columnName.size();
          int count = 0;
          StringBuilder sb = new StringBuilder();
          for (String str : columnName) {
              count++;
              if (sb.length() == 0) {
                  sb.append("create table " + TABLE_NAME + " ( ID text, ");
              }
              if (count != (i)) {
                  sb.append(str + " " + "text,");
              } else {
                  sb.append(str + " " + "text);");
              }
          }
  
          query = (sb.toString()); */
                 query = "create table "+ TABLE_NAME +" ( col1 text, col2 text, col3 text, col4, text);";
        //  CREATE_TABLE= "create table "+tableName+" ( "+"id"+ " text, "+"col1"+" text , "+"col2"+" text, " +"col3"+" text , "+"col4"+" text );";
  
      }
  
      void addEntry(List&lt;String&gt; data,GenericDTO genericDTO)
      {
  
          List&lt;Object&gt; cols = genericDTO.getAttributeValues("custom_expense");
       //   System.out.println("Cols:"+cols.size());
       //   System.out.println("Data"+data.size());
         //List&lt;String&gt; columnName = new ArrayList&lt;&gt;();
          int size = data.size();
          ContentValues values = new ContentValues();
          for(int i =0; i&lt;size;i++) {
             //Toast.makeText(context, cols.get(i).toString(), Toast.LENGTH_SHORT).show();
              values.put(cols.get(i).toString(), data.get(i));
            /*  values.put("col1","a");
              values.put("col2","a");
              values.put("col3","a");
              values.put("col4","a");*/
  
  
          }
          long res = db.insert(TABLE_NAME, null, values);
  
  
          //long res = db.insert(TABLE_NAME,null,values);
          if (res == -1) {
              Toast.makeText(context, "Failed", Toast.LENGTH_SHORT).show();
          } else {
              Toast.makeText(context, "Success", Toast.LENGTH_SHORT).show();
  
          }
      }
  
      private  class ScopeOpenHelper extends SQLiteOpenHelper {
          String DBPATH;
          Context ctx;
          public ScopeOpenHelper(Context context) {
              super(context, DATABASE_NAME, null, DATABASE_VERSION);
          }
  
  
          @Override
          public void onCreate(SQLiteDatabase db) {
              db.execSQL(query);
          }
  
          @Override
          public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  
                   }
      }
  }
  </code></pre>
  
  <p>package com.example.kunj.scope;</p>
  
  <pre><code>import android.app.Dialog;
  import android.os.Bundle;
  import android.support.annotation.Nullable;
  import android.support.design.widget.FloatingActionButton;
  import android.support.v4.app.Fragment;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.view.Window;
  import android.view.WindowManager;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.ListView;
  
  import java.util.ArrayList;
  import java.util.List;
  
  /**
   * Created by kunj on 1/23/2018.
   */
  public class CustomizeFragment extends Fragment {
      Dialog dialog;
      Button addbtn;
      EditText data1,data2,data3,data4;
      ListView customlistview;
      CustomScopeDBHelper customScopeDBHelper;
      GenericDTO genericDTO;
      ScopeDBHelper scopeDBHelper;
      @Nullable
      @Override
      public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
          return inflater.inflate(R.layout.custom_frg,container,false);
      }
  
      @Override
      public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
          super.onViewCreated(view, savedInstanceState);
          customlistview = (ListView) view.findViewById(R.id.customlistview);
          customScopeDBHelper = new CustomScopeDBHelper(getActivity());
          addbtn = (Button) view.findViewById(R.id.addbtn);
          scopeDBHelper = new ScopeDBHelper(getActivity());
  
          data1 = (EditText) view.findViewById(R.id.data1);
          data2 = (EditText) view.findViewById(R.id.data2);
          data3 = (EditText) view.findViewById(R.id.data3);
          data4 = (EditText) view.findViewById(R.id.data4);
          genericDTO = new GenericDTO();
  
          addbtn.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  final String dataOne = data1.getText().toString();
                  final String dataTwo = data2.getText().toString();
                  final String dataThree = data3.getText().toString();
                  final String dataFour = data4.getText().toString();
  
                  List&lt;String&gt; data = new ArrayList&lt;String&gt;();
                  data.add(dataOne);
                  data.add(dataTwo);
                  data.add(dataThree);
                  data.add(dataFour);
                  List&lt;String&gt; columnList = new ArrayList&lt;String&gt;();
                  columnList.add("col1");
                  columnList.add("col2");
                  columnList.add("col3");
                  columnList.add("col4");
                  String TABLE_NAME = "custom_expense";
                  customScopeDBHelper.getQuery(columnList, TABLE_NAME);
                  for(String s:columnList)
                  genericDTO.addAttribute(TABLE_NAME,s);
  
  
                  customScopeDBHelper.addEntry(data,genericDTO);
  
              }
          });
          FloatingActionButton fab = (FloatingActionButton) view.findViewById(R.id.fab);
          fab.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
  
                  dialog = new Dialog(getActivity());
                  dialog.setTitle("Create Record");
                  dialog.setContentView(R.layout.layout_dialog);
                  Window window = dialog.getWindow();
                  window.setLayout(WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.WRAP_CONTENT);
                  dialog.show();
  
  
  
  
  
  
              }
          });
      }
  </code></pre>
  
  <p>Main DBHelper class in which main database is made is running perfectly:</p>
  
  <pre><code>   package com.example.kunj.scope;
  
  import android.content.ContentValues;
  import android.content.Context;
  import android.database.Cursor;
  import android.database.sqlite.SQLiteDatabase;
  import android.database.sqlite.SQLiteOpenHelper;
  import android.view.View;
  import android.widget.Toast;
  
  import java.util.ArrayList;
  import java.util.List;
  
  /**
   * Created by kunj on 10/8/2017.
   */
  
  public class ScopeDBHelper {
  
      public  static final String DATABASE_NAME = "scope.db";
      public  static final int DATABASE_VERSION = 1;
      public static final String TABLE_EXPENSE = "expense_table";
     // public static final String TABLE_ATTENDANCE = "attendance_table";
  
      public static final String COLUMN_ID= "ID";
      public static final String COLUMN_CATEGORY= "CATEGORY";
      public static final String COLUMN_DESCRIPTION= "DESCRIPTION";
      public static final String COLUMN_AMOUNT= "AMOUNT";
      public static final String COLUMN_DATE= "DATE";
  
      public  final String CREATE_TABLE= "create table "+TABLE_EXPENSE+" ( "+COLUMN_ID+ " text, "+COLUMN_CATEGORY+" text , "+COLUMN_DESCRIPTION+" text, " +COLUMN_AMOUNT+" text , "+COLUMN_DATE+" date );";
  
  
      public Context context;
      ScopeOpenHelper scopeOpenHelper;
      public static SQLiteDatabase db;
  
      public ScopeDBHelper(Context context) {
          this.context = context;
          scopeOpenHelper = new ScopeOpenHelper(context);
          db = scopeOpenHelper.getWritableDatabase();
      }
  
      void addExpense(Expense expense){
  
  
  
          ContentValues values = new ContentValues();
          values.put(COLUMN_CATEGORY, expense.getCategory());
          values.put(COLUMN_DESCRIPTION, expense.getDescription());
          values.put(COLUMN_AMOUNT,expense.getAmount());
          values.put(COLUMN_DATE, expense.getDate());
  
          long res = db.insert(TABLE_EXPENSE,null,values);
  
          /*if(res == -1)
          {
              Toast.makeText(context,"Failed",Toast.LENGTH_SHORT).show();
          }
          else
          {
              Toast.makeText(context,"Success",Toast.LENGTH_SHORT).show();
  
          }*/
  
      }
  
          List&lt;Expense&gt; getallExpenses()
      {
          List&lt;Expense&gt; expenses = new ArrayList&lt;&gt;();
          Cursor cursor = db.query(TABLE_EXPENSE,null,null,null,null,null,null);
  
          if(cursor != null &amp;&amp; cursor.moveToFirst())
          {
              do {
                  String category = cursor.getString(cursor.getColumnIndex(COLUMN_CATEGORY));
                  String description = cursor.getString(cursor.getColumnIndex(COLUMN_DESCRIPTION));
                  String amount = cursor.getString(cursor.getColumnIndex(COLUMN_AMOUNT));
                  String date = cursor.getString(cursor.getColumnIndex(COLUMN_DATE));
  
                  expenses.add(new Expense(category,description,amount,date) );
  
              }while (cursor.moveToNext());
  
          }
          return expenses;
      }
  
      public int deleteExpense(Expense ex) {
  
          return db.delete(TABLE_EXPENSE,COLUMN_DESCRIPTION+"=?",new String[]{ex.description});
      }
  
  
      private  class ScopeOpenHelper extends SQLiteOpenHelper {
          public ScopeOpenHelper(Context context) {
              super(context, DATABASE_NAME, null, DATABASE_VERSION);
          }
  
          @Override
          public void onCreate(SQLiteDatabase db) {
  
              db.execSQL(CREATE_TABLE);
              //System.out.print(CREATE_TABLE);
          }
  
          @Override
          public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
  
          }
      }
  
  }
  </code></pre>
  
  <p>The fragment which uses the database is:</p>
  
  <pre><code> package com.example.kunj.scope;
  
  import android.app.DatePickerDialog;
  import android.app.Dialog;
  import android.content.DialogInterface;
  import android.os.Bundle;
  import android.support.annotation.Nullable;
  import android.support.design.widget.FloatingActionButton;
  import android.support.v4.app.Fragment;
  import android.support.v7.app.AlertDialog;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.view.Window;
  import android.view.WindowManager;
  import android.widget.AdapterView;
  import android.widget.ArrayAdapter;
  import android.widget.Button;
  import android.widget.DatePicker;
  import android.widget.EditText;
  import android.widget.ListView;
  import android.widget.Spinner;
  import android.widget.Toast;
  
  import java.text.SimpleDateFormat;
  import java.util.Calendar;
  import java.util.List;
  import java.util.Locale;
  
  /**
   * Created by kunj on 1/23/2018.
   */
  public class ExpenseFragment extends Fragment implements AdapterView.OnItemLongClickListener {
  
      Dialog dialog;
      Spinner categorySpinner;
      ArrayAdapter&lt;String&gt; categoryAdapter;
      EditText descriptionEt,amountEt,dateEt;
      Button addbtn,clearbtn,intentbtn;
      ScopeDBHelper scopeDBHelper;
      List&lt;Expense&gt; expenses;
      ListView expenselistview;
      ExpenseAdapter expenseAdapter;
      Calendar myCalendar;
  
      @Nullable
      @Override
      public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
          return inflater.inflate(R.layout.expense_frg,container,false);
      }
  
      @Override
      public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
  
          super.onViewCreated(view, savedInstanceState);
          expenselistview = (ListView)view.findViewById(R.id.expenselistview);
          scopeDBHelper =  new ScopeDBHelper(getActivity());
          expenses = scopeDBHelper.getallExpenses();
  
          expenseAdapter = new ExpenseAdapter((HomePage) getActivity(),expenses);
          expenselistview.setAdapter(expenseAdapter);
          expenselistview.setOnItemLongClickListener(this);
          myCalendar = Calendar.getInstance();
  
  
          final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
  
              @Override
              public void onDateSet(DatePicker view, int year, int monthOfYear,
                                    int dayOfMonth) {
                  // TODO Auto-generated method stub
                  myCalendar.set(Calendar.YEAR, year);
                  myCalendar.set(Calendar.MONTH, monthOfYear);
                  myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                  updateLabel();
  
  
              }
  
          };
  
  
          FloatingActionButton fab = (FloatingActionButton) view.findViewById(R.id.fab);
          fab.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
  
                  dialog = new Dialog(getActivity());
                  dialog.setTitle("Add Expense");
                  dialog.setContentView(R.layout.layout_dialog);
                  Window window = dialog.getWindow();
                  window.setLayout(WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.WRAP_CONTENT);
                  dialog.show();
  
                  categorySpinner = (Spinner) dialog.findViewById(R.id.category);
                  descriptionEt= (EditText) dialog.findViewById(R.id.description);
                  amountEt = (EditText) dialog.findViewById(R.id.amount);
                  dateEt = (EditText) dialog.findViewById(R.id.date);
                  addbtn = (Button) dialog.findViewById(R.id.addbtn);
                  clearbtn = (Button) dialog.findViewById(R.id.clearbtn);
  
                  String items [] ={"Entertainment","Food","Movie","Goods","Fuel","Other"};
  
  
                  categoryAdapter = new ArrayAdapter&lt;String&gt;(getActivity(),android.R.layout.simple_spinner_dropdown_item,items);
                  categorySpinner.setAdapter(categoryAdapter);
  
  
  
  
                  dateEt.setOnClickListener(new View.OnClickListener() {
  
                      @Override
                      public void onClick(View v) {
  
                          //        Toast.makeText(HomePage.this,"Clicked",Toast.LENGTH_SHORT).show();
  
                          // TODO Auto-generated method stub
                          new DatePickerDialog(getActivity(), date, myCalendar
                                  .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                                  myCalendar.get(Calendar.DAY_OF_MONTH)).show();
                      }
                  });
  
                  addbtn.setOnClickListener(new View.OnClickListener() {
                      @Override
                      public void onClick(View view) {
  
                          final String categorystrng = categorySpinner.getSelectedItem().toString();
                          final String descriptionstrng = descriptionEt.getText().toString();
                          final String amountstrng = amountEt.getText().toString();
                          final String datestrng = dateEt.getText().toString();
  
                          /*if(categorystrng.isEmpty())
                          {
                              categoryEt.setError("Mandatory field");
                              return;
                          }*/
                          if(descriptionstrng.isEmpty())
                          {
                              descriptionEt.setError("Mandatory field");
                              return;
                          }
                          if(amountstrng.isEmpty())
                          {
                              amountEt.setError("Mandatory field");return;
                          }
  
  
  
                          Expense expense = new Expense(categorystrng,descriptionstrng,amountstrng,datestrng);
                          //Toast.makeText(HomePage.this, categorystrng,Toast.LENGTH_SHORT).show();
                          scopeDBHelper.addExpense(expense);
                          expenseAdapter.addExpense(expense);
                          expenseAdapter.notifyDataSetChanged();
                          dialog.dismiss();
                      }
                  });
  
  
              }
          });
  
      }
  
      private void updateLabel() {
          String myFormat = "dd/MM/yy"; //In which you need put here
          SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
  
          dateEt.setText(sdf.format(myCalendar.getTime()));
      }
  
      @Override
      public boolean onItemLongClick(AdapterView&lt;?&gt; adapterView, View view, int i, long l) {
  
         final Expense ex = expenses.get(i);
  
          final AlertDialog.Builder dialog = new AlertDialog.Builder(getContext());
          dialog.setTitle("Delete Expense");
          dialog.setMessage("Do you want to delete this entry?");
  
  
          dialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialogInterface, int i) {
  
  
                  if(scopeDBHelper.deleteExpense(ex)==1)
                  {
                      Toast.makeText(getContext(),"Expense Deleted",Toast.LENGTH_SHORT).show();
                      expenseAdapter.delete(ex);
                      expenseAdapter.notifyDataSetChanged();
  
  
                  }
                  else
                  {
                      Toast.makeText(getContext(),"Couldn't delete this entry",Toast.LENGTH_SHORT).show();
  
                  }
  
  
              }
          });
          dialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialogInterface, int i) {
  
  
              }
          });
  
          dialog.show();
  
          return false;
      }
  }
  </code></pre>