Android 从文件中批量导入数据,个人记录而已

本文记录了使用LitePal框架在Android中批量导入大量数据的过程。通过在MyApplication中配置,利用事务处理16万条数据只需28秒。建立索引可以显著提升查找效率,未建索引需54分钟,而建索引后只需2分钟14秒。数据写入时,一次性写入JSON文件比逐条写入快5秒。数据源存放在res/raw下的ac_account文件中。
摘要由CSDN通过智能技术生成

使用litePal来做为数据库的框架:

0:首先导入litepal 的依赖和fastjson的依赖

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'org.litepal.android:core:1.3.2'
    compile 'com.alibaba:fastjson:1.2.13'
}

1:创建一个MyApplication,

package tech.androidstudio.dbtest;

import org.litepal.LitePalApplication;

/**
 * Created by Kodulf on 2016/6/30.
 */
public class MyApplication extends LitePalApplication {
    @Override
    public void onCreate() {
        super.onCreate();
    }
}


然后修改清单文件:android:name=".MyApplication"

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="tech.androidstudio.dbtest">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:name=".MyApplication"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

2:在main的文件夹下面创建一个assets的文件夹,然后创建一个litepal.xml的文件,


里面的list 是我们下面的创建的类Account,就是会映射为一个表

<?xml version="1.0" encoding="utf-8"?>
<litepal>
    <dbname value="demo" ></dbname>

    <version value="1" ></version>

    <list>
        <mapping class="tech.androidstudio.dbtest.Account"></mapping>
    </list>
</litepal>

3:创建Account的类:记住一定要包含一个空的参数的构造方法,因为下面会用到FastJson转换的时候用到,不然会报错的。

package tech.androidstudio.dbtest;

import org.litepal.crud.DataSupport;

import java.util.Date;

/**
 * Created by Kodulf on 2016/6/30.
 */
public class Account extends DataSupport {
//(uid, acc_no, realname, mobile, attribute, status, cdate, edate)
// VALUES ('20009', null, null, '18329160075', 1, 1, '2012-08-30 15:12:35', '2014-02-28 02:54:25');
    private int uid;
    private String acc_no;
    private String  realname;
    private String mobile;
    private int attribute;
    private int status;
    private Date cdate;
    private Date edate;

    public Account() {
    }

    public Account(int uid, String acc_no, String realname, String mobile, int attribute, int status, Date cdate, Date edate) {
        this.uid = uid;
        this.acc_no = acc_no;
        this.realname = realname;
        this.mobile = mobile;
        this.attribute = attribute;
        this.status &#
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值