title: Insert data into database using AsyncTask
body: <p>Android 2.3.3</p>
  
  <p>I am new to using AsyncTask, so, my implementation may or may not be right. I have to insert data into database using a AsyncTask. </p>
  
  <p>The problem is there are no errors / exceptions, but the database is not being created. I didn't know whether the problem is related to database or asynctask. So I am providing the code below. please have a look.</p>
  
  <p>Database File :::</p>
  
  <pre><code>import android.content.Context;
  import android.database.sqlite.SQLiteDatabase;
  import android.database.sqlite.SQLiteDatabase.CursorFactory;
  import android.database.sqlite.SQLiteOpenHelper;
  
  public class Database_SpeedDial extends SQLiteOpenHelper{
  
      static String name = "speeddial_contacts";
      static int version = 1;
  
      SQLiteDatabase db;
  
      public Database_SpeedDial(Context context) {
          super(context, name, null, version);
          // TODO Auto-generated constructor stub
  
          db = getWritableDatabase();     
      }
  
      @Override
      public void onCreate(SQLiteDatabase db) {
          // TODO Auto-generated method stub
  
          db.execSQL("CREATE TABLE IF NOT EXISTS contacts(count INTEGER, image_url TEXT, name TEXT, number TEXT, bool INTEGER)");     
      }
  
      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
          // TODO Auto-generated method stub
  
      }
  
      public void insert_row(int count, String image_url, String name, String number, int bool) {
          // TODO Auto-generated method stub
  
          db.execSQL("INSERT INTO contacts VALUES('"+ count +"', '"+ image_url +"', '"+ name +"', '"+ number +"', '"+ bool +"')");
      }
  
  
  
  }
  </code></pre>
  
  <p>Here is my java code :::</p>
  
  <pre><code>public class Class_Add_Contact extends Activity implements OnClickListener {
  
  Database_SpeedDial dbObj;
  
  @Override
      public void onClick(View v) {
          // TODO Auto-generated method stub
  
          switch (v.getId()) {
          case R.id.btn_addcontact_add:
  
              System.out.println("Inside onClick");
  
              // Using AsyncTask
              // alImage, alContactName ,.... are ArrayList&lt;String&gt;
              InsertData id = new InsertData(alImage, alContactName, alPhoneNumber, alBoolean); 
              id.execute();
              System.out.println("Finished Inserting");
  
              break;
  
          case R.id.btn_addcontact_cancel:
  
              System.out.println("Cancel button clicked");
  
              break;
  
          default:
              break;
          }
  
      }
  
  
  private class InsertData extends AsyncTask&lt;Void, Void, Void&gt;{
  
          ArrayList&lt;String&gt; alImage_insertdata;
          ArrayList&lt;String&gt; alContactName_insertdata;
          ArrayList&lt;String&gt; alPhoneNumber_insertdata;
          ArrayList&lt;Boolean&gt; alBoolean_insertdata;
  
          public InsertData(ArrayList&lt;String&gt; alImage, ArrayList&lt;String&gt; alContactName, 
                  ArrayList&lt;String&gt; alPhoneNumber, ArrayList&lt;Boolean&gt; alBoolean)
          {
              alImage_insertdata = alImage;
              alContactName_insertdata = alContactName;
              alPhoneNumber_insertdata = alPhoneNumber;
              alBoolean_insertdata = alBoolean;
          }
  
  
          @Override
          protected void onPreExecute() { 
  
              System.out.println("Inside onPreExecute");
          }
  
  
          @Override
          protected Void doInBackground(Void... params) {
              // TODO Auto-generated method stub
  
              System.out.println("Inside doInBackground");
  
                      //dbObj = new Database_SpeedDial(this) -&gt; I used this in onCreate()
                      // of the main class but didn't work, so shifted it here...
                      // This doesn't matter i guess...
  
              dbObj = new Database_SpeedDial(Class_Add_Contact.this);
  
              for(int i=0; i&lt; alBoolean_insertdata.size();i++)
              {
                  if(alBoolean.get(i))
                  {
                      dbObj.insert_row(i+1, alImage_insertdata.get(i).toString(), 
                              alContactName_insertdata.get(i).toString(), 
                              alPhoneNumber_insertdata.get(i).toString(), 
                              1);
                  }
                  else
                  {
                      dbObj.insert_row(i+1, alImage_insertdata.get(i).toString(), 
                              alContactName_insertdata.get(i).toString(), 
                              alPhoneNumber_insertdata.get(i).toString(),
                              0);
                  }
              }
  
              return null;
          }   
  
          @Override
          protected void onPostExecute(Void res) {
  
              System.out.println("Inside onPostExecute");
  
          }
  
  
      }
  
  }
  </code></pre>
  
  <p>Image from DDMS :::</p>
  
  <p><img src="https://i.stack.imgur.com/qNKD9.png" alt="enter image description here"></p>
  
  <p>The code executes without any problem, but the database is not created. What could be the problem? </p>