Java tutorial
//package com.java2s; import android.provider.BaseColumns; import android.text.TextUtils; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.List; public class Main { private final static StringBuffer mBuffer = new StringBuffer(); public static String createTableSql(Class<?> classs, String tableName, String... extraColumns) { mBuffer.setLength(0); mBuffer.append("CREATE TABLE IF NOT EXISTS "); if (!TextUtils.isEmpty(tableName)) { mBuffer.append(tableName); } else { String className = classs.getSimpleName(); if (className.contains("[]")) { throw new IllegalArgumentException("Can not create array class table"); } mBuffer.append(classs.getSimpleName()); } mBuffer.append("(" + BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "); Field[] fields = classs.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { Field field = fields[i]; if (field.getType() == Integer.class) { mBuffer.append(field.getName() + " INTEGER,"); } else if (field.getType() == Double.class || field.getType() == Float.class) { mBuffer.append(field.getName() + " REAL,"); } else if (field.getType() == String.class) { mBuffer.append(field.getName() + " TEXT,"); } else if (field.getType() == Boolean.class) { mBuffer.append(field.getName() + " INTEGER,"); } else if (field.getType() == List.class || field.getType() == ArrayList.class) { mBuffer.append(field.getName() + " TEXT,"); } } if (extraColumns != null && extraColumns.length != 0) { for (int i = 0; i < extraColumns.length; i++) { mBuffer.append(extraColumns[i]); if (i != extraColumns.length - 1) { mBuffer.append(","); } else { mBuffer.append(")"); } } } return mBuffer.toString(); } }