title: Fast XML to database
body: <p>I'd like to insert some xml data into my database. First I created an array of some objects that I passed to my DBAdapter in order to insert them in the DB with a transaction, as shown below in <code>insertArticles</code>. Now I'd like to insert directly my data while parsing them, in order to avoid the creation of the array of objects in memory. The only way I see to do it is to make my SQLiteDatabase object public and to make the transaction directly in my XML parser. It just doesn't seem very elegant to have some DB actions outside my DBAdapter.</p>
  
  <p>What's the standard way of efficiently loading XML data into a database?</p>
  
  <p>Thanks</p>
  
  <pre><code>public class DBAdapter {
  
      /* ... */
  
      private DatabaseHelper mDbHelper;
      private SQLiteDatabase mDb;
  
      private static class DatabaseHelper extends SQLiteOpenHelper {
  
          DatabaseHelper(Context context) {
              super(context, DATABASE_NAME, null, DATABASE_VERSION);
          }
  
          @Override
          public void onCreate(SQLiteDatabase db) {
              db.execSQL(CREATE_TABLE_CAT);
          }
  
          @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 categories");
              onCreate(db);
          }
      }
  
      public DBAdapter(Context ctx) {
          this.mCtx = ctx;
      }
  
      public DBAdapter open() throws SQLException {
          mDbHelper = new DatabaseHelper(mCtx);
          mDb = mDbHelper.getWritableDatabase();
          return this;
      }
  
      public void close() {
          mDbHelper.close();
      }   
  
      public void insertArticles(List&lt;Article&gt; articles) {
  
          mDb.beginTransaction();
  
          try{
  
              for(Article article : articles){
  
  
                  ContentValues initialValues = new ContentValues();
                  initialValues.put(KEY_EMID, article.id);
                  initialValues.put(KEY_TYPE_INFO, article.type_info);
                  initialValues.put(KEY_CATEGORY_ID, article.category_id);
                  initialValues.put(KEY_TITLE, article.title);
                  initialValues.put(KEY_INTRO, article.intro);
                  initialValues.put(KEY_URL_PIC, article.url_pic);
                  initialValues.put(KEY_URL_ARTICLE, article.url_article);
                  initialValues.put(KEY_DATE, article.date);
                  initialValues.put(KEY_USER_FIRSTNAME, article.user_firstname);
                  initialValues.put(KEY_USER_LASTNAME, article.user_lastname);
                  initialValues.put(KEY_NUM_COMMENTS, article.num_comments);
                  initialValues.put(KEY_CONTENT, article.content);
                  initialValues.put(KEY_IS_FAVORITE, 0);              
  
                  String query = "SELECT " + KEY_NAME + " FROM " + DATABASE_TABLE_CAT + " WHERE " +
                  KEY_EMID + "=" + article.category_id;
                  Cursor cur = mDb.rawQuery(query, null);
                  if(cur.moveToFirst()){
                      initialValues.put(KEY_CATEGORY_NAME, cur.getString(0));
                  }
                  else{
                      initialValues.put(KEY_CATEGORY_NAME, mCtx.getResources().getText(R.string.menu_home).toString());
                  }
                  cur.close();
  
                  mDb.insert(DATABASE_TABLE_ART, null, initialValues);
  
              }
  
              mDb.setTransactionSuccessful();
          } catch (SQLException e) {
              mDb.endTransaction();
              throw e;
          } finally {
              mDb.endTransaction();
          }
      }
  }
  </code></pre>