Created
August 19, 2018 05:11
-
-
Save NsAveek/eed832cb49c72b8e7c8e27562254315c to your computer and use it in GitHub Desktop.
The magic method createTable() to handle creation of the given fields automatically
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
private void createTable(String tableName, Map<String, String> fields, SQLiteDatabase db) { | |
Iterator iter = fields.entrySet().iterator(); | |
String columns = "("; | |
Log.d("column", columns); | |
int counter = 1; | |
while (iter.hasNext()) { | |
Map.Entry mEntry = (Map.Entry) iter.next(); | |
columns += mEntry.getKey() + " " + mEntry.getValue(); | |
if (counter == fields.size()) { | |
columns += ")"; | |
} else { | |
columns += " , "; | |
counter++; | |
} | |
} | |
if (counter > 1) { | |
Log.d("column", columns); | |
String createNewTable = "CREATE TABLE IF NOT EXISTS " + tableName + " " + columns; | |
db.execSQL(createNewTable); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://gist.github.com/NsAveek/13db76c988cb201c2294745dd8afaf51
This is the actual DatabaseHelper.java class