build Create Table Statement - Android Database

Android examples for Database:Table Create

Description

build Create Table Statement

Demo Code


import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import android.content.ContentValues;
import android.database.Cursor;
import android.util.Log;

public class Main{
    private static final String TAG = SQLiteHelper.class.getSimpleName();
    public static String buildCreateTableStatement(Class<?> clazz) {
        if (clazz == null) {
            return null;
        }/*from w  w w  .  jav a 2s .  c o m*/

        final List<Field> fields = new ArrayList<Field>();

        final Field[] declaredFields = clazz.getDeclaredFields();
        if (declaredFields != null && declaredFields.length > 0) {
            fields.addAll(Arrays.asList(declaredFields));
        }

        final Class<?> superclass = clazz.getSuperclass();
        if (superclass != null) {
            Field[] superFields = superclass.getDeclaredFields();
            if (superFields != null && superFields.length > 0) {
                fields.addAll(Arrays.asList(superFields));
            }
        }

        if (fields == null || fields.size() == 0) {
            return null;
        }

        final StringBuilder columnsBuilder = new StringBuilder("");
        for (int i = 0; i < fields.size(); i++) {
            final Field field = fields.get(i);
            final String columnDeclareString = buildColumnDeclareStatement(field);
            if (columnDeclareString != null) {
                columnsBuilder.append(columnDeclareString);
                columnsBuilder.append(",");
            }
        }
        if (columnsBuilder.length() == 0) {
            return null;
        }
        columnsBuilder.deleteCharAt(columnsBuilder.length() - 1);

        final StringBuilder createTableStatementBuilder = new StringBuilder(
                "");
        createTableStatementBuilder.append("CREATE TABLE ");
        createTableStatementBuilder.append(clazz.getSimpleName());
        createTableStatementBuilder.append(" (");
        createTableStatementBuilder.append(columnsBuilder.toString());
        createTableStatementBuilder.append(" );");
        String createTableQuery = createTableStatementBuilder.toString();
        Log.d(TAG, String.format("Create table statement: \n%s",
                createTableQuery));
        return createTableQuery;
    }
    private static String buildColumnDeclareStatement(Field field) {
        final String sqliteType = mapToSQLiteType(field.getType()
                .toString());
        if (sqliteType != null) {
            final StringBuilder methodDeclareBuilder = new StringBuilder("");
            methodDeclareBuilder.append(field.getName());
            methodDeclareBuilder.append(" ");
            methodDeclareBuilder.append(sqliteType);
            if (field.getName().equals("_id")) {
                methodDeclareBuilder.append(" primary key autoincrement");
            }
            return methodDeclareBuilder.toString();
        }
        return null;
    }
    private static String mapToSQLiteType(String type) {
        if (type.equals("int") || type.contains("java.lang.Integer")
                || type.equals("long") || type.contains("java.lang.Long")
                || type.equals("boolean")
                || type.contains("java.lang.Boolean")) {
            return "INTEGER";
        } else if (type.equals("float") || type.contains("java.lang.Float")
                || type.equals("double")
                || type.contains("java.lang.Double")) {
            return "REAL";
        } else if (type.contains("java.lang.String")) {
            return "TEXT";
        }
        return null;
    }
}

Related Tutorials