Skip to content

Instantly share code, notes, and snippets.

@Basuhampali
Last active January 1, 2016 09:28
Show Gist options
  • Save Basuhampali/f8ab67e3e6fc2b2973b9 to your computer and use it in GitHub Desktop.
Save Basuhampali/f8ab67e3e6fc2b2973b9 to your computer and use it in GitHub Desktop.
Creating Data Base tables in Android programatically
public static String createSQLiteTable() {
TableCreater tableCreator[]= {
new TableCreater(KEY_ID ,TableCreater.integerDataType),
new TableCreater(KEY_TODO,TableCreater.textDataType),
new TableCreater(KEY_STATUS ,TableCreater.integerDataType)
};
return MySQLiteHelper.createTable(TABLE_NAME, tableCreator);
}
/**
* Send TableCreater {@link array} object and get the table string
* @param tableName
* @param tableCreator {@link array} object
* @return table {@link string}
* @throws <code>null</code> if sent TableCreater {@link array} object size is not greater than one.
*/
public static String createTable(String tableName,TableCreater tableCreator[]) {
if(tableCreator.length > 1) {
String CREATE_TABLE = "create table ";
CREATE_TABLE = CREATE_TABLE.concat(tableName + "(" + tableCreator[0].parameterName + " integer primary key autoincrement, ");
for(int i=1;i<tableCreator.length;i++) {
if(i == tableCreator.length-1) {
CREATE_TABLE = CREATE_TABLE.concat(tableCreator[i].parameterName + " "+ tableCreator[i].dataType + " not null " + ");");
}
else {
CREATE_TABLE = CREATE_TABLE.concat(tableCreator[i].parameterName + " "+ tableCreator[i].dataType + " not null, "); }
}
return CREATE_TABLE;
}
return null;
}
public class TableCreater {
public static String integerDataType = "integer";
public static String textDataType = "text";
public static String realDataType = "real";
public String parameterName;
public String dataType;
public TableCreater(String string, String string2) {
// TODO Auto-generated constructor stub
this.parameterName = string;
this.dataType = string2;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment