I am looking to set up my app to dynamically create table based on user input. But I want to have a defined structure for the tables and the user will essentially name the table.
foe example, I want the user to define the name of the tables they want, but all of the tables will have the same amount and name of rows that they will fill out through another settings option.
here is my db, I currently have two tables in there which I need to change to have the menucategory table will be the table that gets created by the user, and the menuitem table will be the columns of that category table
public class DatabaseHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
protected static final String DATABASE_NAME = "Store";
private static final String sqlcat ="menucategory";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE menuitem " +
"( id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"name TEXT, " +
"description TEXT, "+
"price int, "+
"cost int, "+
"cat TEXT)";
db.execSQL(sql);
String sqlcat = "CREATE TABLE menucategory " +
"( id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"name TEXT)";
db.execSQL(sqlcat);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String sql = "DROP TABLE IF EXISTS menuitem";
db.execSQL(sql);
onCreate(db);
String sqlcat = "DROP TABLE IF EXISTS menucategory";
onCreate(db);
}
/**
* Getting all names
* returns list of mobile name
* */
public List<String> getAllNames(){
List<String> names = new ArrayList<String>();
// Select All Query
String selectQuery = "SELECT * FROM " + sqlcat;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
names.add(cursor.getString(1));
} while (cursor.moveToNext());
}
// closing connection
cursor.close();
db.close();
// returning names
return names;
}
}