Created
August 8, 2011 15:15
-
-
Save elevine/1131934 to your computer and use it in GitHub Desktop.
Use within an Android SQLiteOpenHelper to generate database tables
This file contains hidden or 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
| /** | |
| * 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