热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Android-加速插入数据库中的数据。-Android-Speedupinsertingdataindatabase

IcurrentlyhaveaCSVfilethatIparseandamtryingtoinsertthedataintotheandroiddatabase.

I currently have a CSV file that I parse and am trying to insert the data into the android database. The problem I am having is that it is taking way too long to insert all of the data. It's a good amount of data but I feel like it shouldn't take 20min or so to complete.

我现在有一个CSV文件,我解析并试图将数据插入到android数据库中。我的问题是,要插入所有数据的时间太长了。这是一个很好的数据,但我觉得不应该花20分钟左右完成。

Basically, I create my database, then begin the parsing. While parsing through each individual CSV row, I grab the required data and insert it into the database. In total there are around 40000 rows.

基本上,我创建了我的数据库,然后开始解析。在解析每个CSV行时,我获取所需的数据并将其插入到数据库中。总共大约有40000行。

Is there any way I can speed up this process? I have tried batch inserts but it never really helped (unless I did it wrong).

有什么办法可以加快这个过程吗?我尝试过批量插入,但它从来没有真正的帮助(除非我做错了)。

Code down below.

下面的代码。

Thanks.

谢谢。

DatabaseHelper (i have two insert commands based on the amount of data in each csv row):

DatabaseHelper(根据每个csv行中的数据量,我有两个insert命令):

// add zipcode
    public void add9Zipcode(String zip, String city, String state, String lat,
            String longi, String decom) {

        // get db and content values
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();

        db.beginTransaction();
        try{

            // add the values
            values.put(KEY_ZIP, zip);
            values.put(KEY_STATE, state);
            values.put(KEY_CITY, city);
            values.put(KEY_LAT, lat);
            values.put(KEY_LONG, longi);
            values.put(KEY_DECOM, decom);

            // execute the statement
            db.insert(TABLE_NAME, null, values);

            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }

        db.close();

    }

    public void add12Zipcode(String zip, String city, String state, String lat,
            String longi, String decom, String tax, String pop, String wages) {

        // get db and content values
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();

        db.beginTransaction();
        try{
            // add the values
            values.put(KEY_ZIP, zip);
            values.put(KEY_STATE, state);
            values.put(KEY_CITY, city);
            values.put(KEY_LAT, lat);
            values.put(KEY_LONG, longi);
            values.put(KEY_DECOM, decom);
            values.put(KEY_TAX, tax);
            values.put(KEY_POP, pop);
            values.put(KEY_WAGES, wages);

            // execute the statement
            db.insert(TABLE_NAME, null, values);

            db.setTransactionSuccessful();
        } finally{
            db.endTransaction();  
        }


        db.close();
}

Parse File:

解析文件:

public void parse(ArrayList theArray, DatabaseHandler db) {

        String[] data = null;

        // while loop to get split the data into new lines
        // for loop to split each string in the array list of zipcodes
        for (int x = 0; x 

1 个解决方案

#1


3  

Refer to this answer I made previously: Inserting 1000000 rows in sqlite3 database

请参考我之前给出的答案:在sqlite3数据库中插入1000000行

In short, use an InsertHelper and do more than one insert per transaction - unless you did something wonky, the speed increase should be noticeable.

简而言之,使用插入器,并在每个事务中执行多个插入操作——除非您做了一些不确定的事情,否则速度的增加应该是显而易见的。

Edit:
In short:

编辑:简而言之:

  1. Your SQLiteOpenHelper should be a singleton used across your entire application.
  2. 您的SQLiteOpenHelper应该是跨整个应用程序使用的单例。
  3. Don't go around calling close() on your SQLiteDatabase instance - it's cached in the SQLiteOpenHelper and every time you close you force the helper to reopen it.
  4. 不要在SQLiteDatabase实例上调用close()—它缓存在SQLiteOpenHelper中,每次关闭时都强制helper重新打开它。
  5. Batch your inserts, start a transaction outside the call to the addZipCode methods and mark it as successful after you've done all the inserts - then commit the transaction.
  6. 对插入进行批处理,在调用addZipCode方法之外启动事务,并在完成所有插入之后将其标记为成功,然后提交事务。
  7. Use an InsertHelper - it will format the insert properly as a prepared statement and is nice and reusable.
  8. 使用InsertHelper -它将正确地格式化插入作为一个准备好的语句,并且是好的和可重用的。
  9. Be mindful of synchronizing access to the database - unless you intend to do all your database work on the UI-thread (which is not recommended) - you either need to enable locking or guard access to the database to avoid concurrent access.
  10. 注意对数据库的同步访问——除非您打算在ui线程上完成所有的数据库工作(这是不推荐的)——您需要启用对数据库的锁定或保护访问,以避免并发访问。

推荐阅读
  • 欢乐的票圈重构之旅——RecyclerView的头尾布局增加
    项目重构的Git地址:https:github.comrazerdpFriendCircletreemain-dev项目同步更新的文集:http:www.jianshu.comno ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • Android JSON基础,音视频开发进阶指南目录
    Array里面的对象数据是有序的,json字符串最外层是方括号的,方括号:[]解析jsonArray代码try{json字符串最外层是 ... [详细]
  • 标题: ... [详细]
  • 本文介绍了Python爬虫技术基础篇面向对象高级编程(中)中的多重继承概念。通过继承,子类可以扩展父类的功能。文章以动物类层次的设计为例,讨论了按照不同分类方式设计类层次的复杂性和多重继承的优势。最后给出了哺乳动物和鸟类的设计示例,以及能跑、能飞、宠物类和非宠物类的增加对类数量的影响。 ... [详细]
  • 本文介绍了在CentOS上安装Python2.7.2的详细步骤,包括下载、解压、编译和安装等操作。同时提供了一些注意事项,以及测试安装是否成功的方法。 ... [详细]
  • 本文介绍了在处理不规则数据时如何使用Python自动提取文本中的时间日期,包括使用dateutil.parser模块统一日期字符串格式和使用datefinder模块提取日期。同时,还介绍了一段使用正则表达式的代码,可以支持中文日期和一些特殊的时间识别,例如'2012年12月12日'、'3小时前'、'在2012/12/13哈哈'等。 ... [详细]
  • 本文介绍了在MFC下利用C++和MFC的特性动态创建窗口的方法,包括继承现有的MFC类并加以改造、插入工具栏和状态栏对象的声明等。同时还提到了窗口销毁的处理方法。本文详细介绍了实现方法并给出了相关注意事项。 ... [详细]
  • 上图是InnoDB存储引擎的结构。1、缓冲池InnoDB存储引擎是基于磁盘存储的,并将其中的记录按照页的方式进行管理。因此可以看作是基于磁盘的数据库系统。在数据库系统中,由于CPU速度 ... [详细]
  • 十大经典排序算法动图演示+Python实现
    本文介绍了十大经典排序算法的原理、演示和Python实现。排序算法分为内部排序和外部排序,常见的内部排序算法有插入排序、希尔排序、选择排序、冒泡排序、归并排序、快速排序、堆排序、基数排序等。文章还解释了时间复杂度和稳定性的概念,并提供了相关的名词解释。 ... [详细]
  • 本文分析了Wince程序内存和存储内存的分布及作用。Wince内存包括系统内存、对象存储和程序内存,其中系统内存占用了一部分SDRAM,而剩下的30M为程序内存和存储内存。对象存储是嵌入式wince操作系统中的一个新概念,常用于消费电子设备中。此外,文章还介绍了主电源和后备电池在操作系统中的作用。 ... [详细]
  • 生成式对抗网络模型综述摘要生成式对抗网络模型(GAN)是基于深度学习的一种强大的生成模型,可以应用于计算机视觉、自然语言处理、半监督学习等重要领域。生成式对抗网络 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
author-avatar
jgfioirejmf
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有