菜单

Android 用SQLite实现业务之道

2018年11月16日 - sqlite

复制代码 代码如下:

介绍一个ORM框架,LitePal。
GitHub:https://github.com/LitePalFramework/LitePal
以方法十分简单,导入xx包就不说了。

 public void payment() {
  SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
  db.beginTransaction();//开始作业
  try {
   db.execSQL(“update person set amount=amount-10 where personid=2”);
   db.execSQL(“update person set amount=amount+10 where personid=5”);
   db.setTransactionSuccessful();//
设置工作的标志吧true,调用此方法会在履到endTransaction()方法是交给业务,若没有调用此方法会在实行到endTransaction()方法回滚事务。
  } catch (SQLException e) {
  } finally {
   db.endTransaction();
  }
  // 结束工作,有一定量种状况:commit,rollback,
  //
事务之交付或回滚是出于工作的标志决定的,如果事情之表明也true就会见交到,否则回滚,默认情况下业务之表明吧false
 } 

  1. 创建一个接近,继承DataSupport。

PS:类似转账功能,两单操作以同一个事务中就。

若或许感兴趣之稿子:

public class StudentBean extends DataSupport {

    private int studentID;
    private String studentName;
    private String studentAge;
    private String studentSex;
    private String studentPhone;

    public int getStudentID() {
        return studentID;
    }

    public void setStudentID(int studentID) {
        this.studentID = studentID;
    }

    public String getStudentName() {
        return studentName;
    }

    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }

    public String getStudentAge() {
        return studentAge;
    }

    public void setStudentAge(String studentAge) {
        this.studentAge = studentAge;
    }

    public String getStudentSex() {
        return studentSex;
    }

    public void setStudentSex(String studentSex) {
        this.studentSex = studentSex;
    }

    public String getStudentPhone() {
        return studentPhone;
    }

    public void setStudentPhone(String studentPhone) {
        this.studentPhone = studentPhone;
    }
}
  1. assets目录下开创一个誉为也litepal.xml的部署文件

<?xml version="1.0" encoding="utf-8"?>
<litepal>
    <dbname value="litepal" >
    </dbname>
    <version value="3" >
    </version>
    <list>
         <mapping class="com.jeremy.universal_android.sqlite.bean.StudentBean"></mapping>
    </list>
</litepal>
  1. application继承LitePalApplication

public class UniversalAndroidApplication extends LitePalApplication {
    @Override
    public void onCreate() {
        super.onCreate();
//        初始化sqlite库
        LitePalApplication.initialize(this);
    }
}

哪些运用?

private void insertWithTs() {
    StudentBean studentBean = null;

    for (int i = 0; i < 1000; i++) {
        studentBean = new StudentBean();

        studentBean.setStudentName("小明" + i);
        studentBean.setStudentAge("" + i);

        studentBean.save();
    }
}

private void insertWithoutTs() {

    List<StudentBean> studentBeanList = new ArrayList<>();

    StudentBean studentBean = null;

    for (int i = 0; i < 1000; i++) {
        studentBean = new StudentBean();

        studentBean.setStudentName("小明" + i);
        studentBean.setStudentAge("" + i);

        studentBeanList.add(studentBean);
    }

    DataSupport.saveAll(studentBeanList);
}

题目来了,上面两独办法一致是插1000长达数据,区别是啊为?insertWithTs()调用的是目标的save(),执行1000次等;而insertWithoutTs()是拿1000个目标在一个凑中,最后调用平等坏saveAll()。

扣押一下少种方式效率如何。
insertWithTs(),单位凡毫秒

E/SQLITE: begin --> 1470285148375
E/SQLITE: end --> 1470285156347
E/SQLITE: gap --> 7972

insertWithoutTs(),单位是毫秒

E/SQLITE: begin --> 1470285043075
E/SQLITE: end --> 1470285045001
E/SQLITE: gap --> 1926

距离如此的老,原因在乌?看源码
save()方法是这样的

public synchronized void saveThrows() {
    SQLiteDatabase db = Connector.getDatabase();
    db.beginTransaction();
    try {
        // some code
        db.setTransactionSuccessful();
    } catch (Exception e) {
        throw new DataSupportException(e.getMessage());
    } finally {
        db.endTransaction();
    }
}

而saveAll()呢?

public static synchronized <T extends DataSupport> void saveAll(Collection<T> collection) {
    SQLiteDatabase db = Connector.getDatabase();
    db.beginTransaction();
    try {
        //code to save collection
        db.setTransactionSuccessful();
    } catch (Exception e) {
        throw new DataSupportException(e.getMessage());
    } finally {
        db.endTransaction();
    }
}

各个一样不良save()都干到一个业务的begin和end,当数据量变充分时,代码执行效率就是会见明白低于saveAll()的法。另外需要注意的是,数据插入属于耗时操作,应开启线程。

相关文章

标签:

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图