2012-01-17 83 views
0

我正在使用SAX解析器解析网络上的XML文件。 解析时我想将数据添加到数据库中,因此我使用INSERT查询来添加数据。但每个插入需要:10 - 15毫秒,我有近150个记录,几乎需要130秒来解析和插入。 我已经尝试过在事务中插入查询,但它仍然给我同一时间。我附上我的代码。我不知道我是否正确解析它,或者我的插入事务是错误的?SAX解析器解析需要很长时间才能插入数据库中

XMLHandler.java

public class XMLHandler extends DefaultHandler { 

private static boolean inKey = false; 
private static boolean inCode = false; 
private static boolean inTitle = false; 
private static boolean inType = false; 
private static boolean inRoom = false; 
private static boolean inDescription = false; 
private static boolean inStart = false; 

private List List = new List(); 

public void startElement(String uri, String name, String qName, 
    Attributes atts) { 

if (name.trim().equals("key")) 
    inKey = true; 
else if (name.trim().equals("code")) 
    inCode = true; 
else if (name.trim().equals("title")) 
    inTitle = true; 
else if (name.trim().equals("type")) 
    inType = true; 
else if (name.trim().equals("room")) 
    inRoom = true; 
else if (name.trim().equals("description")) 
    inDescription = true; 
else if (name.trim().equals("start")) 
    inClassStart = true; 

} 

public void endElement(String uri, String name, String qName) 
    throws SAXException { 

if (name.trim().equals("key")) 
    inKey = false; 
else if (name.trim().equals("code")) 
    inCode = false; 
else if (name.trim().equals("title")) 
    inTitle = false; 
else if (name.trim().equals("type")) 
    inType = false; 
else if (name.trim().equals("room")) 
    inRoom = false; 
else if (name.trim().equals("description")) 
    inDescription = false; 
else if (name.trim().equals("start")) 
    inClassStart = false; 
} 

public void characters(char ch[], int start, int length) { 

String chars = (new String(ch).substring(start, start + length)); 

try { 
    if(inKey) 
      List.key = chars; 
    if(inCode) 
     List.code = chars; 
    if(inTitle) 
     List.title = chars; 
    if(inType) 
     List.type = chars; 
    if(inRoom) 
     List.room = chars; 
    if(inDescription) 
     List.description = chars; 
    if(inStart) 
     List.start = chars; 

    DB.insertFeed(List.key, List.code, List.title, List.type, List.room, List.description, List.start); 

} catch (Exception e) { 
    Log.e("NewsDroid", e.toString()); 
} 

} 

DatabaseManager.java

public void insertFeed(String key, String code, String title, String type, String room, String desc,String start) { 


    db.beginTransaction(); 
     try{ 
      String sql = "INSERT OR REPLACE INTO " + TEST+ "(KEY,CODE,TITLE, TYPE ,ROOM , DESCRIPTION, START) VALUES" + "(?,?,?,?,?,?,?);"; 
      Object [] bindArgs = new Object[]{key,code,title,type,room, desc,start}; 
      db.execSQL(sql, bindArgs);  
      db.setTransactionSuccessful(); 
} 
    catch (SQLException e){} 

    finally{ 

     db.endTransaction(); 
    } 
} 

回答

3

要插入每次一行,可能会做db.open()和db.close()每一次。
而不是尝试使用“InsertHelper”。
粗用法示例如下图所示:

private void insertTweetSourcesInBulk(ArrayList tweetSources, boolean replace) { InsertHelper ih = new InsertHelper(db, TABLE_NAME_TWEET_SOURCE);

final int idIndex = ih.getColumnIndex(Audio._ID); 
    final int sequenceNumIndex = ih.getColumnIndex(TweetSource.SEQUENCE_NUM); 
    final int thumbnailUrlIndex = ih.getColumnIndex(TweetSource.THUMBNAIL_URL); 
    final int titleIndex = ih.getColumnIndex(TweetSource.TITLE); 

    for (TweetSource source : tweetSources) { 
     Logger.log(TAG, "Inserting id: " + source.getId()); 

     if (replace) { 
      ih.prepareForReplace(); 
     } else { 
      ih.prepareForInsert(); 
     } 

     ih.bind(idIndex, Integer.parseInt(source.getId())); 
     ih.bind(sequenceNumIndex, Float.parseFloat(source.getSequenceNum())); 
     ih.bind(thumbnailUrlIndex, source.getThumbnailUrl()); 
     ih.bind(titleIndex, source.getTitle()); 

     ih.execute(); 
    } 

    ih.close(); 
} 



+0

但是,然后在ArrayList解析时单独添加字符串也需要时间吗? – Change 2012-01-17 19:02:03

+0

是的,但这种方法肯定会为你节省一些时间,因为它同时在db中插入多条记录。此外,如果您可以使用JSON格式进行回复,则可以使用GSON或JACKSON解析器加速解析(我更喜欢杰克逊)。 – akkilis 2012-01-17 19:09:05

+0

是否有可能,如果你可以更多地解释我如何添加ih.bind(idIndex,Integer.parseInt(source.getId()));这意味着什么是getId()函数,它有什么作用? – Change 2012-01-17 21:51:28

1

你插入语句看的权利。你的数据库是否有适当的索引?因为每个INSERT语句都有一个对现有行的隐式查询,因为它必须确定行是否已经存在。如果你的代码中的“key”确实是主键,那么它有一个隐式索引,但如果情况并非如此......那么这可能会给你带来麻烦。

另外,您应该知道数据库操作本质上是缓慢的。有一件事我不明白,如果每个INSERT都花费15ms,那么150个INSERT如何花费130秒?

另外,一定要看看下面的akkilis'响应(关于InsertHelper),它允许你一次插入多行到你的数据库[并且如果这对你有用,一定要注册]。这将要求你在将数据提供给数据库之前将其存储在临时缓冲区中,这应该不是很困难。

上InsertHelper更多信息: http://developer.android.com/reference/android/database/DatabaseUtils.InsertHelper.html

+0

现在肯定会尝试这部分!因为我现在插入的方式很疯狂。这需要很多时间! – Change 2012-01-17 19:08:04

0

不要做每插入一个交易。为整个INSERT集执行一个事务。每笔交易都涉及闪存I/O,速度很慢。