2016-04-08 7 views
1

SQLiteデータベースに約400件の挿入があり、これには数秒かかります。私は、beginTransaction()とendTransaction()を使用して周囲の挿入がそれを多く最適化するという赤を持っています。しかし、私はこれを行う方法を理解していない、今私はそれを行うしようとした方法は、全く何も挿入されていない結果になります。助けてください。データベース設定でトランザクションを使用するにはどうすればいいですか?

package com.example.oscar.strengthapp; 

import android.content.ContentValues; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteStatement; 

import java.util.ArrayList; 

/** 
* Created by Oscar on 2016-01-09. 
*/ 
public class WorkoutDB extends AbstractWorkoutDB{ 
    private static final WorkoutDB ourInstance = new WorkoutDB(); 
    private WorkoutDB() {} 
    public static WorkoutDB getInstance() { 
     return ourInstance; 
    } 

    public void beginTransaction(){ 
     database.beginTransaction(); 
    } 
    public void endTransaction(){ 
     database.endTransaction(); 
    } 
    public void insertExercise(int workoutID, String name, double weight, int sets, int reps){ 
     ContentValues values = new ContentValues(); 
     values.put(KEY_WORKOUT, workoutID); 
     values.put(KEY_NAME, name); 
     values.put(KEY_REPS, reps); 
     values.put(KEY_SETS, sets); 
     values.put(KEY_WEIGHT, weight); 
     long rowId = database.insert(DB_TABLE_WORK, null, values); 
    } 

    public boolean updateExercise(Exercise exercise){ 
     ContentValues values = new ContentValues(); 
     values.put(KEY_WORKOUT, exercise.getWorkoutID()); 
     values.put(KEY_NAME, exercise.getName()); 
     values.put(KEY_REPS, exercise.getReps()); 
     values.put(KEY_SETS, exercise.getSets()); 
     values.put(KEY_WEIGHT, exercise.getWeight()); 
     return database.update(DB_TABLE_WORK, values, KEY_EXERCISE_ID + 
       "=" + exercise.getExerciseID(), null) > 0; 
    } 

    public boolean deleteExercise(Exercise exercise){ 
     return database.delete(DB_TABLE_WORK, KEY_EXERCISE_ID + 
       "=" + exercise.getExerciseID(), null) >0; 
    } 

    public void clearTable(){ 
     database.delete(DB_TABLE_WORK, null, null); 
    } 
    public ArrayList<Exercise> getAllExercises(){ 
     Cursor cr = getAllExercisesCursor(); 
     return makeExerciseListFromCursor(cr); 
    } 

    public ArrayList<Exercise> getWorkout(int id){ 
     Cursor cr = getWorkoutCursor(id); 
     return makeExerciseListFromCursor(cr); 
    } 

    private ArrayList<Exercise> makeExerciseListFromCursor(Cursor cr){ 
     ArrayList<Exercise> exercises = new ArrayList<Exercise>(); 
     if(cr != null && cr.moveToFirst()) 
      do{ 
       exercises.add(new Exercise(cr.getLong(0), cr.getInt(1), cr.getString(2), 
         cr.getDouble(3), cr.getInt(4), cr.getInt(5))); 
      }while (cr.moveToNext()); 
     return exercises; 
    } 

    protected void createTestData() { 

     System.out.println("Inserting Test Data"); 
     WorkoutDB.getInstance().insertExercise(1, "Bench", 0.75, 5, 5); 
     WorkoutDB.getInstance().insertExercise(1, "Squat", 0.75, 5, 5); 
     WorkoutDB.getInstance().insertExercise(2, "Deadlift", 0.8, 3, 2); 
     WorkoutDB.getInstance().insertExercise(2, "Squat", 0.75, 5, 5); 
     WorkoutDB.getInstance().insertExercise(3, "Bench", 0.85, 3, 3); 
     WorkoutDB.getInstance().insertExercise(3, "Squat", 0.85, 3, 3); 
    } 
} 

package com.example.oscar.strengthapp; 

import android.content.Context; 
import android.database.Cursor; 
import android.database.SQLException; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 

/** 
* Created by Oscar on 2016-01-09. 
*/ 
public abstract class AbstractWorkoutDB { 
    public static final String DB_TABLE_WORK = "Work"; 
    public static final String KEY_EXERCISE_ID = "exerciseId"; 
    public static final String KEY_WORKOUT = "workoutId"; 
    public static final String KEY_NAME = "name"; 
    public static final String KEY_WEIGHT = "weight"; 
    public static final String KEY_REPS = "reps"; 
    public static final String KEY_SETS = "sets"; 
    public static final String TAG = "WorkoutDB"; 
    public static final String DB_NAME = "Workouts"; 
    protected SQLiteDatabase database; 
    private static final String [] columns = { 
      KEY_EXERCISE_ID, 
      KEY_WORKOUT, 
      KEY_NAME, 
      KEY_WEIGHT, 
      KEY_REPS, 
      KEY_SETS}; 
    private static final int DB_VERSION = 1; 
    private MyDbHelper mDbHelper; 
    private Context context; 

    private static final String DB_CREATE_WORK = "create table " + 
      DB_TABLE_WORK + " (" + 
      KEY_EXERCISE_ID + " INTEGER PRIMARY KEY, " + 
      KEY_WORKOUT + " INTEGER, " + 
      KEY_NAME + " TEXT, " + 
      KEY_WEIGHT + " DOUBLE, " + 
      KEY_SETS + " INTEGER, " + 
      KEY_REPS + " INTEGER);"; 

    protected abstract void createTestData(); // must be implemented 

    public void open(boolean writable, Context ctx) throws SQLException 
    { 
     this.context = ctx; 
     mDbHelper = new MyDbHelper(context); 
     if (writable) 
     { 
      database = mDbHelper.getWritableDatabase(); 
      if(mDbHelper.isFirstTime()) 
       System.out.println("is first time"); 
       //createTestData(); 
     } 
     else 
      database = mDbHelper.getReadableDatabase(); 
    } 
    public void close() 
    { 
     mDbHelper.close(); 
    } 


    protected Cursor getAllExercisesCursor() 
    { // select * from song 
     return database.query(DB_TABLE_WORK, columns, null, null, null, null, null); 
    } 
    protected Cursor getWorkoutCursor(int id){ 
     return database.query(
       DB_TABLE_WORK, 
       columns, 
       KEY_WORKOUT + "=?", 
       new String[]{String.valueOf(id)}, 
       null, null, null, null); 
    } 

    protected static class MyDbHelper extends SQLiteOpenHelper // Local help class 
    { 
     private boolean firstTime = false; 
     MyDbHelper(Context context) 
     { 
      super(context, DB_NAME, null, DB_VERSION); 
     } 
     public void onCreate(SQLiteDatabase sqldb) // called if needed 
     { 
      sqldb.execSQL(DB_CREATE_WORK); 
      firstTime = true; 
     } 
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
     { 
      db.execSQL("DROP TABLE IF EXISTS " + DB_TABLE_WORK); 
      onCreate(db); 
     } 
     public boolean isFirstTime() 
     { 
      return firstTime; 
     } 

    } 
} 

と、これは私の方法は、魔女のように見えるものでは挿入

あなたの変更がコミットされるのを前に、 endTransaction() setTransactionSuccessful()を呼び出す必要が
public void initiate(String program, int length){ 
    SharedPreferences.Editor editor = prefs.edit(); 
    editor.putInt("CURRENT_WORKOUT", 1); 
    editor.putString("CURRENT_PROGRAM", program); 
    editor.putInt("CURRENT_LENGTH", length); 
    editor.apply(); 
    WorkoutDB.getInstance().clearTable(); 
} 
public void juggernaut(){ 
    initiate("Juggernaut Method", 112); 
    WorkoutDB.getInstance().beginTransaction(); 
    WorkoutDB.getInstance().insertExercise(1, "Squat", 0.6, 5, 10); 
    WorkoutDB.getInstance().insertExercise(1, "RDL", 0, 3, 12); 
    WorkoutDB.getInstance().insertExercise(1, "Lunges", 0, 3, 20); 
    WorkoutDB.getInstance().insertExercise(1, "Plank", 0, 3, 30); 

    WorkoutDB.getInstance().insertExercise(2, "REST", 0, 0, 0); 
    //and 400more of the same 
    WorkoutDB.getInstance().endTransaction(); 
    } 

答えて

0

の束を行います。 database.getWritableDatabase();(オープンデータベースコマンド)後の開始時に

+0

ありがとうございますsetTransactionSucessful()は欠けていたものです! –

0

あなたはdatabase.close();(クローズdatabaseコマンド)を呼び出す前に、あなたは、スキーマそう

database.setTransactionSuccessful(); 
database.endTransaction(); 

を呼び出す

database.beginTransaction(); 

と終わりを呼び出します

database.getWritableDatabase(); 
    database.beginTransaction(); 

    //Insert, Update, Delete commands 

    database.setTransactionSuccessful(); 
    database.endTransaction(); 
    database.close(); 
+0

ありがとうございますsetTransactionSuccessful();行方不明だったものですが、私はそれを再構築するでしょう、それはより意味をなさないようです。 –

0

標準i取引のためのDIOMはdocumentationに記載されています:

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

例外はそれをバイパスし、ロールバックを引き起こすように、別のコールsetTransactionSuccessful()が必要です。

関連する問題