由于跳过帧而丢失记录

时间:2017-09-22 02:36:08

标签: android sqlite csv

我正在使用的应用程序是从服务器获取.csv(20k-30k记录)中的数据,它需要将数据保存到SQLiteDatabase中。

它有效,但有些记录丢失,似乎已被跳过。

String sql = "INSERT INTO " + tableName
                + " VALUES (?,?,?,?,?,?);";
        SQLiteDatabase db = openHelper.getWritableDatabase();
        SQLiteStatement statement = db.compileStatement(sql);

        try {
            db.beginTransaction();
            String[] sa = null;
            for (final String csvline : arrCSV) {
                statement.clearBindings();
                sa = csvline.split(",");
                if(sa.length==6){
                    statement.bindString(1, sa[0]);
                    statement.bindString(2, sa[1]);
                    statement.bindString(3, sa[2]);
                    statement.bindString(4, sa[3]);
                    statement.bindString(5, sa[4]);
                    statement.bindString(6, sa[5]);
                }
                statement.execute();
            }
            db.setTransactionSuccessful();
            Log.d("Transaction", "Successful");
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            statement.releaseReference();
            statement.close();
            db.endTransaction();            
            db.releaseMemory();
        }

我知道这个错误表明由于负载很重,内存消耗非常高。是否有更有效的方法来保存SQLiteDatabase中的数据,而不是经典的CSV访问并从那里处理它?

在DB中写入的代码

select_one

更新

收集中未加载缺失的记录。 跳帧是罪魁祸首吗? 集合中的加载只是对csv文件的简单解析 有时不可复制,所以我认为这是由于跳帧。

1 个答案:

答案 0 :(得分:0)

我认为这个问题与跳帧和< 100帧被认为是小/不重要的数字。至少根据The application may be doing too much work on its main thread

我经常看到它并且从未成为任何问题的原因。我甚至看到它除了将一个活动的结果返回到刚刚开始第二个活动的活动之外什么也没做。

正如您所评论的那样, split 产生的元素数量有时不是6.问题很可能就是在这种情况下不会发生插入,也许由于约束(没有看到如何定义列,只能进行猜测)。

但是,您似乎认为csvline中的每一行应分为6个元素。你应该调查为什么不呢?

为了研究我建议在分割之前获得原始数据的细节以及分割之后的结果数据,只要分割创建的元素数量不是6。通过改变: -

            sa = csvline.split(",");
            if(sa.length==6){
                statement.bindString(1, sa[0]);
                statement.bindString(2, sa[1]);
                statement.bindString(3, sa[2]);
                statement.bindString(4, sa[3]);
                statement.bindString(5, sa[4]);
                statement.bindString(6, sa[5]);
            }
            statement.execute();

            sa = csvline.split(",");
            if(sa.length==6){
                statement.bindString(1, sa[0]);
                statement.bindString(2, sa[1]);
                statement.bindString(3, sa[2]);
                statement.bindString(4, sa[3]);
                statement.bindString(5, sa[4]);
                statement.bindString(6, sa[5]);
            } else {
                Log.d("NOT6SPLIT","CSVLINE WAS ===>" + csvline + "<===");
                Log.d("NOT6SPLIT","CSVLINE WAS SPLIT INTO " + Integer.toString(sa.length) + " ELEMENTS :-");
                for(String s: sa) {
                    Log.d("NOT6SPLIT","\tElement Value ===>" + s + "<===");
                }
            }
            statement.execute();

statement.execute()更改为: -

           if (statement.executeInsert() < 1) {
               Log.d("INSERTFAIL","Couldn't insert where CSVLINE was ===>" + csvline + "<===");
           }

也可以协助('executeInsert'返回插入记录的 rowid ,否则 -1 ,不确定使用{{1}定义的表的后果})。

如果问题归结为包含 WITHOUT ROWID 认为特殊字符或 metacharaceters 的字符的数据,那么我一点也不会感到惊讶: -

  

there are 12 characters   特殊含义:

     
      
  • 反斜杠split
  •   
  • 插入符\
  •   
  • 美元符号^
  •   
  • 句点或点$
  •   
  • 垂直条或竖线符号.
  •   
  • 问号|
  •   
  • 星号或星号?
  •   
  • 加号*
  •   
  • 左括号+
  •   
  • 右括号(
  •   
  • 开方括号)
  •   
  • 和开口大括号[
  •   
     

这些特殊字符通常称为“元字符”。最   单独使用时会出现错误。