title: How to store parsed JSON data into SQLite
body: <p>I have written code to parse JSON Data into ListView, but now before showing data into ListView, I would like to store it to SQLite database and then want to show in a List.</p>
  
  <p>And to store data into SQLite, I wrote Database helper class as well, but now i am little bit confuse how to use this class to store parsed data directly into sqlite</p>
  
  <pre><code>public class MainActivity extends Activity {
  
  ArrayList&lt;Actors&gt; actorsList;   
  ActorAdapter adapter;
  
  SQLiteDB sqliteDB;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      sqliteDB = new SQLiteDB (this);
  
      actorsList = new ArrayList&lt;Actors&gt;();
  
      new JSONAsyncTask().execute("http://microblogging.wingnity.com/JSONParsingTutorial/jsonActors");
  
      ListView listview = (ListView)findViewById(R.id.list);
      adapter = new ActorAdapter(getApplicationContext(), R.layout.row, actorsList);      
      listview.setAdapter(adapter);       
      listview.setOnItemClickListener(new OnItemClickListener() {
  
          @Override
          public void onItemClick(AdapterView&lt;?&gt; arg0, View arg1, int position,
                  long id) {
              // TODO Auto-generated method stub
              Toast.makeText(getApplicationContext(), actorsList.get(position).getName(), Toast.LENGTH_LONG).show();              
          }
      });
  }
  
  
  class JSONAsyncTask extends AsyncTask&lt;String, Void, Boolean&gt; {
  
      ProgressDialog dialog;
  
      @Override
      protected void onPreExecute() {
          super.onPreExecute();
          dialog = new ProgressDialog(MainActivity.this);
          dialog.setMessage("Loading, please wait");
          dialog.setTitle("Connecting server");
          dialog.show();
          dialog.setCancelable(false);
      }
  
      @Override
      protected Boolean doInBackground(String... urls) {
          try {
  
              //------------------&gt;&gt;
              HttpGet httppost = new HttpGet(urls[0]);
              HttpClient httpclient = new DefaultHttpClient();
              HttpResponse response = httpclient.execute(httppost);
  
              int status = response.getStatusLine().getStatusCode();
  
              if (status == 200) {
                  HttpEntity entity = response.getEntity();
                  String data = EntityUtils.toString(entity);
  
                  JSONObject jsono = new JSONObject(data);
                  JSONArray jarray = jsono.getJSONArray("actors");
  
                  for (int i = 0; i &lt; jarray.length(); i++) {
                      JSONObject object = jarray.getJSONObject(i);
  
                      Actors actor = new Actors();                        
                      actor.setName(object.getString("name"));
  
                      actorsList.add(actor);
                  }
                  return true;
              }
  
              //------------------&gt;&gt;
  
          } catch (ParseException e1) {
              e1.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          } catch (JSONException e) {
              e.printStackTrace();
          }
          return false;
      }
  
      protected void onPostExecute(Boolean result) {
          dialog.cancel();
          adapter.notifyDataSetChanged();
          if(result == false)
              Toast.makeText(getApplicationContext(), "Unable to fetch data from server", Toast.LENGTH_LONG).show();
      }
    }
  }
  </code></pre>
  
  <p><strong>SQLite</strong></p>
  
  <pre><code>public class SQLiteDB {
  
      public static final String KEY_ID = "id";
      public static final String KEY_NAME = "name";
  
      private static final String TAG = "DBAdapter";
      private static final String DATABASE_NAME = "SQLiteDB";
  
      private static final String TABLE_NAME = "sample";
      private static final int DATABASE_VERSION = 1;
  
      private static final String CREATE_TABLE = 
              "create table sample (id text primary key autoincrement, name text not null);";
  
      private final Context context;
      private DatabaseHelper DBHelper;
      private SQLiteDatabase db;
  
      public SQLiteDB(Context ctx) {
  
          this.context = ctx;
          DBHelper = new DatabaseHelper(context);
      }
  
      private static class DatabaseHelper extends SQLiteOpenHelper {
          DatabaseHelper(Context context) {           
          super(context, DATABASE_NAME, null, DATABASE_VERSION);
      }
  
      @Override
      public void onCreate(SQLiteDatabase db) {           
          try {
                  db.execSQL(CREATE_TABLE);
              } catch (SQLException e) {
                  e.printStackTrace();
              }
      }
  
      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {      
          Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                  + newVersion + ", which will destroy all old data");
          db.execSQL("DROP TABLE IF EXISTS sample");
          onCreate(db);
          }
      }
  
      //---open SQLite DB---
      public SQLiteDB open() throws SQLException {    
          db = DBHelper.getWritableDatabase();
          return this;
      }
  
      //---close SQLite DB---
      public void close() {   
          DBHelper.close();
      }
  
      //---insert data into SQLite DB---
      public long insert(String name) {
          ContentValues initialValues = new ContentValues();
          initialValues.put(KEY_NAME, name);      
          return db.insert(TABLE_NAME, null, initialValues);
      }
  
      //---Delete All Data from table in SQLite DB---
      public void deleteAll() {
          db.delete(TABLE_NAME, null, null);
      }
  
      //---Get All Contacts from table in SQLite DB---
      public Cursor getAllData() {
          return db.query(TABLE_NAME, new String[] {KEY_NAME}, 
                  null, null, null, null, null);
      }
  
  }
  </code></pre>