Android Open Source - simple-sqlite S Q Lite Open Helper App






From Project

Back to project page simple-sqlite.

License

The source code is released under:

Apache License

If you think the Android project simple-sqlite 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 com.aldobo.simple.sqlite;
//from   w ww  . j a v a 2 s  .c o m
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.aldobo.simple.sqlite.entities.Field;
import com.aldobo.simple.sqlite.entities.Schema;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


public class SQLiteOpenHelperApp extends SQLiteOpenHelper {

    private final String TAG ="SQliteManager";

    private SQLiteConfiguration mConfiguration;

    public SQLiteOpenHelperApp(Context context, SQLiteConfiguration configuration)
    {
        super(context,configuration.getDatabaseName(),null,configuration.getDatabaseVersion());
        mConfiguration = configuration;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        Iterator<Schema> schemas = mConfiguration.getSchemas();
        List<Field> indexFields = new ArrayList<Field>();

        while (schemas.hasNext())
        {
            Schema schema = schemas.next();
            StringBuilder stringBuilder = new StringBuilder();
            String separator = "";
            for(Field field:schema.getFields())
            {
               if(field.isIndex())
                   indexFields.add(field);

               stringBuilder.append(separator + field.getSQliteCreateRepresentation());
               separator = ",";
            }
            String query = String.format("CREATE TABLE [%s] (%s);",schema.getTableName(),stringBuilder.toString());
            db.execSQL(query);
            for(Field field:indexFields)
            {
                query = String.format("CREATE INDEX '%s' ON [%s]('%s')",field.getIndex(),schema.getTableName(),field.getName());
                db.execSQL(query);
            }
        }

        if(mConfiguration.hasExtension())
        {
            mConfiguration.getExtension().onCreate(db);
        }

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Iterator<Schema> schemas = mConfiguration.getSchemas();
        while (schemas.hasNext())
        {
            Schema schema = schemas.next();
            String query = String.format("DROP TABLE IF EXISTS [%s];",schema.getTableName());
            db.execSQL(query);
        }

        if(mConfiguration.hasExtension())
        {
            mConfiguration.getExtension().onUpgrade(db, oldVersion, newVersion);
        }

        onCreate(db);
    }
}




Java Source Code List

com.aldobo.simple.sqlite.SQLiteConfiguration.java
com.aldobo.simple.sqlite.SQLiteManager.java
com.aldobo.simple.sqlite.SQLiteOpenHelperApp.java
com.aldobo.simple.sqlite.SQLiteOpenHelperExtendable.java
com.aldobo.simple.sqlite.entities.Field.java
com.aldobo.simple.sqlite.entities.Model.java
com.aldobo.simple.sqlite.entities.SQLiteDefaultValue.java
com.aldobo.simple.sqlite.entities.SQLiteType.java
com.aldobo.simple.sqlite.entities.Schema.java
com.aldobo.simple.sqlite.sample.MainActivity.java
com.aldobo.simple.sqlite.sample.MainApplication.java
com.aldobo.simple.sqlite.sample.fragments.BaseFragment.java
com.aldobo.simple.sqlite.sample.fragments.MyListFragment.java
com.aldobo.simple.sqlite.sample.models.Person.java
com.aldobo.simple.sqlite.tags.Column.java
com.aldobo.simple.sqlite.tags.Table.java
com.aldobo.simple.sqlite.utils.ModelIterable.java
com.aldobo.simple.sqlite.utils.Utils.java