Android Sqlite Performance

前端 未结 3 1052
遇见更好的自我
遇见更好的自我 2020-12-05 01:33

I have been doing some experiments to measure sqlite performance on android. I was disappointed a little bit with the results. What i did was inserting 10.000 queries to ta

3条回答
  •  旧巷少年郎
    2020-12-05 01:47

    You can use SQL transactions in Android like this. It's better to insert multiple rows into the database in larger batches then making single commit (write into SQLlite datafile which is very slow) for every inserted row.

    public void insert(List students)
    {
        SQLiteDatabase db = sh.getWritableDatabase();
        ContentValues cv = new ContentValues();
    
        db.beginTransaction();
    
        try {
            for (Student s : students) {
                cv.put(StudentHelper.FIRSTNAME,s.getFirstName());
                cv.put(StudentHelper.LASTNAME,s.getLastName());
                cv.put(StudentHelper.ADDRESS,s.getAddress());
                cv.put(StudentHelper.GPA,s.getGpa());
    
                db.insertOrThrow(StudentHelper.TABLE_NAME, null, cv)
            }
            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }
    }
    

提交回复
热议问题