Android Open Source - SimpleContentProvider Simple Database Helper






From Project

Back to project page SimpleContentProvider.

License

The source code is released under:

Apache License

If you think the Android project SimpleContentProvider listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package dk.simplecontentprovider;
/*from   ww  w .ja va 2  s.  c  o  m*/
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.util.List;

public class SimpleDatabaseHelper extends SQLiteOpenHelper {
    private final List<SimpleContentProvider.Entity> mEntities;

    public SimpleDatabaseHelper(Context context, String databaseName, int databaseVersion, List<SimpleContentProvider.Entity> entities) {
        super(context, databaseName, null, databaseVersion);
        this.mEntities = entities;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        for (SimpleContentProvider.Entity entity : mEntities) {
            String entitySql = null;

            for (SimpleContentProvider.EntityColumn column : entity.columns) {
                if (entitySql == null) {
                    entitySql = column.name + " " + column.definition;
                } else {
                    entitySql += "," + column.name + " " + column.definition;
                }
            }

            for (String constraint : entity.constraints) {
                if (entitySql == null) {
                    entitySql = constraint;
                } else {
                    entitySql += "," + constraint;
                }
            }

            db.execSQL("CREATE TABLE " + entity.name + " (" + entitySql + ")");
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        for (SimpleContentProvider.Entity entity : mEntities) {
            db.execSQL("DROP TABLE IF EXISTS " + entity.name);
        }
        onCreate(db);
    }
}




Java Source Code List

dk.simplecontentprovider.ContentProviderForTests.java
dk.simplecontentprovider.ContractForTests.java
dk.simplecontentprovider.FullTestSuite.java
dk.simplecontentprovider.SimpleContentProvider.java
dk.simplecontentprovider.SimpleDatabaseHelper.java
dk.simplecontentprovider.SimpleUriMatcher.java
dk.simplecontentprovider.TestContentProvider.java
dk.simplecontentprovider.demo.OverviewActivity.java
dk.simplecontentprovider.demo.OwnersActivity.java
dk.simplecontentprovider.demo.PetsActivity.java
dk.simplecontentprovider.demo.dialogs.AddOwnerDialog.java
dk.simplecontentprovider.demo.dialogs.AddPetDialog.java
dk.simplecontentprovider.demo.provider.DemoContentProvider.java
dk.simplecontentprovider.demo.provider.DemoContract.java