For me SQlite syntax is quite confusing and difficult job . For creating below String
private static final String CREATE_TABLE_TODO = "CREATE TABLE "
+ TABLE_TODO + "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_TODO
+ " TEXT," + KEY_STATUS + " INTEGER" ;
So created a function , which will create SQlite syntac for table creation
And created object , which need to be passed to the function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |