开发者

Fast XML to database

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 insertArticles. 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.

What's the standard way of efficiently loading XML data into a database?

Thanks

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<Article> 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_CATEGOR开发者_如何学JAVAY_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();
        }
    }
}


In my experiences, don't put XML into database unless you cannot find any better solution.

EDIT:

First, let's thing that you need to keep info receiving from some WebService.

Second, you want your data is simple enough, raw data, removing all junks.

Third, you'd want to keep them persistent, protect it safely.

If I were you, I will parse XML data apart when receiving it from WebService, create a database schema which is similar to XML data structure. Next is to map parsed info to DB schema. So I can get the desired info I want from database, not everything. It's good when data is already filtered in good piece of information to use.

I think you're already know these stuffs and this method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜