Skip to content

Instantly share code, notes, and snippets.

@elevine
Created August 8, 2011 15:15
Show Gist options
  • Select an option

  • Save elevine/1131934 to your computer and use it in GitHub Desktop.

Select an option

Save elevine/1131934 to your computer and use it in GitHub Desktop.
Use within an Android SQLiteOpenHelper to generate database tables
/**
* Generates a ddl statement to create a table and executes it
*
* @param db
* @param tablename
* @param columnToType - a mapping of column names to their SQL types in the table
*/
private void createTable(SQLiteDatabase db, String tablename, Map<String,String> columnToType){
StringBuilder builder = new StringBuilder();
builder.append("CREATE TABLE " + tablename +" (");
Iterator<String> iter = columnToType.keySet().iterator();
while(iter.hasNext()){
String column = iter.next();
String type = columnToType.get(column);
builder.append(column).append(" ").append(type);
if(iter.hasNext()) builder.append(",");
}
builder.append(");");
db.execSQL(builder.toString());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment