ngageoint / geopackage-android

GeoPackage Android Library
http://ngageoint.github.io/geopackage-android
MIT License
94 stars 33 forks source link

SQlite Transactioin #50

Closed sunshine0576 closed 5 years ago

sunshine0576 commented 5 years ago

The SQLite database supports transactional operations, but the current version of Geopackage is too cumbersome to use.

SQLiteDatabase db=featureDao.getDb().getDb().getDb();

SQLiteDatabase db1= featureDao.getDatabaseConnection().getDb();

db.beginTransaction();  //Manual Setup to Start Transaction
    try{
        //Batch processing operation
        for(Collection c:colls){
            insert(db, c);
        }
        //Setting the transaction to be processed successfully will automatically roll back uncommitted if not set.
        db.setTransactionSuccessful();      
    }catch(Exception e){

    }finally{
        db.endTransaction(); //处理完成
    }
bosborn commented 5 years ago

You are welcome to utilize the connection directly as follows.

boolean successful = Math.random() < 0.5 ? true : false;
int countBefore = featureDao.count();
int rows = 10;

SQLiteDatabase db = featureDao.getDatabaseConnection().getDb();
db.beginTransaction();
try{
    for(int i = 0; i < rows; i++) {
        FeatureRow row = featureDao.newRow();
        GeoPackageGeometryData geometry = new GeoPackageGeometryData(
                geometryColumns.getSrsId());
        geometry.setGeometry(new Point(0, 0));
        row.setGeometry(geometry);
        featureDao.insert(row);
    }
    if(successful) {
        db.setTransactionSuccessful();
    }
}catch(Exception e){

}finally{
    db.endTransaction();
}

int countAfter = featureDao.count();
TestCase.assertEquals(successful ? countBefore + rows : countBefore, countAfter);

The other connection is for common GeoPackage core and ORM Lite operations.