create Table SQL statement - Android Database

Android examples for Database:Table Create

Description

create Table SQL statement

Demo Code


import android.text.TextUtils;

public class Main{
    //from  w  ww  . ja  va 2  s  . c o  m
    public static String createTable(String tableName, String[] columns,
            String primaryKey) {

        if (TextUtils.isEmpty(tableName)) {
            return null;
        }

        if (columns == null || columns.length == 0) {
            return null;
        }

        if (TextUtils.isEmpty(primaryKey)) {
            primaryKey = "_id";
        }

        StringBuffer sb = new StringBuffer();
        sb.append("CREATE TABLE  IF NOT EXISTS `").append(tableName)
                .append("` (");
        for (String column : columns) {
            sb.append("`").append(column).append("` VARCHAR, ");
        }
        if ("_id".equals(primaryKey)) {
            sb.append("`").append(primaryKey)
                    .append("` INTEGER PRIMARY KEY AUTOINCREMENT)");
        } else {
            sb.append("`").append(primaryKey).append("` TEXT PRIMARY KEY)");
        }
        return sb.toString();
    }
    
    public static String createTable(String tableName, String[] columns) {
        return createTable(tableName, columns, "_id");
    }
}

Related Tutorials