2

私はAndroidが初めてです。4 つのテーブルを含む Android の Sqlite データベースに 6000 レコードを挿入する際に問題があります。1 つのテーブルにレコードを挿入することは、他のテーブルに他のレコードを挿入することを意味します。レコードの数を考えると、トランザクションを使用する必要があることがわかりましたが、データベース関連のクラスで採用されている構造では (ロックのため) 不可能です。

簡単にするために、コードの下に Pois と Images (および Helper) を挿入します。正しいアプローチは何ですか?静的ヘルパー? 静的 SQLite データベース? みんなありがとう!

ヘルパー

    public class PoiDatabaseHelper extends SQLiteOpenHelper {

        private static final String DB_NAME = "poi_database"; 
        private static final int DB_VERSION = 1; 

        public PoiDatabaseHelper(Context context) { 
            super(context, DB_NAME, null, DB_VERSION); 
        } 

        @Override
        public void onCreate(SQLiteDatabase db) { 
            String poiTable = ""; 
            poiTable += "CREATE TABLE poi ("; 
            poiTable += " poi_id INTEGER,"; 
            poiTable += " name TEXT NOT NULL,"; 
            poiTable += " description_en TEXT,"; 
            poiTable += " description_it TEXT,"; 
            poiTable += " time TEXT,"; 
            poiTable += " address TEXT,";
            poiTable += " latitude TEXT,"; 
            poiTable += " logitude TEXT,"; 
            poiTable += " primary key  (poi_id)";
            poiTable += ")"; 

            String imgTable = "";
            imgTable += "CREATE TABLE image ("; 
            imgTable += " image_id INTEGER,"; 
            imgTable += " url TEXT,"; 
            imgTable += " thumb INTEGER,";
            imgTable += " poi_id INTEGER NOT NULL,";
            imgTable += " primary key  (image_id),";
            imgTable += " foreign key (poi_id) references poi(poi_id) on delete set NULL on update cascade";
            imgTable += ")";
} 

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
        //TODO
    }

}

POI インサート

public class PoiDaoImpl implements PoiDao {

    private Context context;
    private PoiDatabaseHelper poiDatabaseHelper;
    private SQLiteDatabase poiDatabase;

    public PoiDaoImpl(Context context) {
        this.context = context;
        poiDatabaseHelper = new PoiDatabaseHelper(context);
        poiDatabase = poiDatabaseHelper.getWritableDatabase();

    }

    @Override
    public void insert(Poi poi) {
        ContentValues values = new ContentValues(); 

        values.put("poi_id", poi.getId());
        values.put("name", poi.getName());
        values.put("description_en", poi.getDescriptionEn());
        values.put("description_it", poi.getDescriptionIt());
        values.put("time", poi.getTime());
        values.put("address", poi.getAddress());
        values.put("latitude", poi.getCoordinates().getLatitude());
        values.put("logitude", poi.getCoordinates().getLongitude());


        ImageDao imageDao = new ImageDaoImpl(context);
        for(Image image: poi.getImages()) {
            if(!image.getUrl().equals("")) {
                imageDao.insert(image, poi.getId());
            }
        }

        long id = poiDatabase.insert("poi", null, values); 
        //poiDatabase.close();

        //poiDatabaseHelper.close();

    }


    @Override
    public void insertAllPois(List<Poi> pois) {
        poiDatabase.beginTransaction();
        try {
            for(Poi poi: pois) {
                insert(poi);
            }
            poiDatabase.setTransactionSuccessful();
        } finally {
            poiDatabase.endTransaction();
        }


    }
}

画像挿入

public class ImageDaoImpl implements ImageDao {

    private Context context;
    private PoiDatabaseHelper poiDatabaseHelper;
    private SQLiteDatabase poiDatabase;


    public ImageDaoImpl(Context context) {
        this.context = context;
        poiDatabaseHelper = new PoiDatabaseHelper(context);
        poiDatabase = poiDatabaseHelper.getWritableDatabase();

    }

    @Override
    public void insert(Image image, int idPoi) {
        ContentValues values = new ContentValues(); 
        values.put("url", image.getUrl());
        if(image.getThumb()) {
            values.put("thumb", 1);
        }
        else {
            values.put("thumb", 0);
        }

        values.put("poi_id", idPoi);
        //poiDatabase = poiDatabaseHelper.getWritableDatabase();
        long id = poiDatabase.insert("image", null, values);
        poiDatabase.close();
    }
}
4

1 に答える 1