title: I am creating an android app and connecting it with the database. Android programming[ the new boston tutorial no 49-54]
body: <p>I am creating an android app and trying to connect it with the database. I am following TheNewBoston on Youtube for the code. I have successfully created the app. </p>
  
  <p><strong>But when I run it, on Insert Query, my app hangs up and doesn't respond.</strong> </p>
  
  <p>Here is the code:</p>
  
  <p><strong>MyDBHandler.java</strong></p>
  
  <pre><code>public class MyDBHandler extends SQLiteOpenHelper {
  public static final int DATABASE_VERSION = 2;
  private static final String DATABASE_NAME = "newproducts.db";
  public static final String TABLE_NAME = "products_table";
  public static final String COLUMN_ID = "_id";
  public static final String COLUMN_NAME = "product_name";
  
  
  public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
      super(context, DATABASE_NAME, factory, DATABASE_VERSION);
  }
  
  @Override
  public void onCreate(SQLiteDatabase db) {
      String query = "CREATE TABLE " +TABLE_NAME+ "("+
              COLUMN_ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+
              COLUMN_NAME+ " TEXT "+
              ");";
      db.execSQL(query);
  
  }
  
  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
      db.execSQL("DROP TABLE IF EXISTS " +TABLE_NAME);
      onCreate(db);
  }
  public void add(Products products){
      ContentValues contentValues = new ContentValues();
      contentValues.put(COLUMN_NAME, products.get_productname());
      SQLiteDatabase db = getWritableDatabase();
      db.insert(TABLE_NAME,null,contentValues);
      db.close();
  }
  
  public String dbToString(){
      SQLiteDatabase db = getWritableDatabase();
      String dbString = "";
      String query = "SELECT * FROM "+TABLE_NAME+";";
  
      Cursor cursor = db.rawQuery(query,null);
      cursor.moveToFirst();
  
      while(!cursor.isAfterLast()){
          if(cursor.getString(cursor.getColumnIndex(""+COLUMN_NAME+""))!=null){
              dbString+= cursor.getString(cursor.getColumnIndex(""+COLUMN_NAME+""));
              dbString += "\n";
          }
      }
      db.close();
      return dbString;
  }
  }
  </code></pre>
  
  <p><strong>MainActivity.Java</strong></p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  EditText mkInput;
  TextView  mkText;
  MyDBHandler dbHandler;
  public int dbVersion;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      mkInput = (EditText)findViewById(R.id.mkInput);
      mkText = (TextView)findViewById(R.id.textView);
      dbVersion = MyDBHandler.DATABASE_VERSION;
      dbHandler = new MyDBHandler(this, null, null, dbVersion);
      printDatabase();
  }
  
  public void addClicked(View view){
      Products products =  new Products(mkInput.getText().toString());
      dbHandler.add(products);
      printDatabase();
  }
  public void deleteClicked(View view){
      Toast.makeText(MainActivity.this,"This is a toast",Toast.LENGTH_LONG).show();
  }
  
  public void printDatabase(){
      String dbString = dbHandler.dbToString();
      mkText.setText(dbString);
      mkInput.setText("");
  }
  }
  </code></pre>
  
  <p><strong>Products.java</strong></p>
  
  <pre><code>public class Products  {
  private int _id;
  private String _productname;
  
  public Products(String _productname) {
      this._productname = _productname;
  }
  
  public void set_id(int _id) {
      this._id = _id;
  }
  
  public void set_productname(String _productname) {
      this._productname = _productname;
  }
  
  public String get_productname() {
      return _productname;
  }
  
  public int get_id() {
      return _id;
  }
  }
  </code></pre>
  
  <p><strong>activity_main.xml</strong></p>
  
  <pre><code>&lt;EditText
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/mkInput"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true"
      android:width="300dp" /&gt;
  
  &lt;Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Add"
      android:id="@+id/addButton"
      android:layout_below="@+id/mkInput"
      android:layout_alignParentLeft="true"
      android:layout_alignParentStart="true"
      android:layout_marginTop="48dp"
      android:onClick="addClicked"/&gt;
  
  &lt;Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Delete"
      android:id="@+id/deleteButton"
      android:layout_alignBottom="@+id/addButton"
      android:layout_alignParentRight="true"
      android:layout_alignParentEnd="true"
      android:onClick="deleteClicked"/&gt;
  
  &lt;TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:textAppearance="?android:attr/textAppearanceLarge"
      android:text="Default Text"
      android:id="@+id/textView"
      android:layout_centerVertical="true"
      android:layout_centerHorizontal="true" /&gt;
  </code></pre>
  
  <p><strong>Output Screen</strong>
  <a href="http://i.stack.imgur.com/aX97C.png" rel="nofollow">This is the app interface</a></p>
  
  <p><strong>On Clicking AddButton, App hangs up</strong></p>