Skip to content

Instantly share code, notes, and snippets.

@NsAveek
Created August 19, 2018 05:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NsAveek/eed832cb49c72b8e7c8e27562254315c to your computer and use it in GitHub Desktop.
Save NsAveek/eed832cb49c72b8e7c8e27562254315c to your computer and use it in GitHub Desktop.
The magic method createTable() to handle creation of the given fields automatically
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);
}
}
@NsAveek
Copy link
Author

NsAveek commented Aug 19, 2018

https://gist.github.com/NsAveek/13db76c988cb201c2294745dd8afaf51

This is the actual DatabaseHelper.java class

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment