动态创建具有已定义行的SQLite表

时间:2016-03-17 12:41:37

标签: android sqlite

我希望设置我的应用程序,以根据用户输入动态创建表。但我希望为表定义一个结构,用户将基本命名表。

例如,我希望用户定义他们想要的表的名称,但是所有表都将具有相同的行数和名称,它们将通过其他设置选项填写。

这里是我的数据库,我目前有两个表,我需要更改,以使menucategory表成为用户创建的表,而menuitem表将是该类别表的列

公共类DatabaseHandler扩展了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;
}

}

1 个答案:

答案 0 :(得分:1)

我不知道你做了什么,但是为了动态创建表我使用这个函数。

public void createUserTable(String tableName) {
    final SQLiteDatabase db = getWritableDatabase();
    String CREATE_TABLE_NEW_USER = "CREATE TABLE " + tableName + " (" + COLUMN_ID + " INTEGER PRIMARY KEY,"+ COLUMN_NAME + " TEXT)";
    db.execSQL(CREATE_TABLE_NEW_USER);
    db.close();
}

在您的评论之后,这是一个答案......

String TABLE_MENU_ITEM_LIST = "MenuItemList";  // change as per your requirement 
String CREATE_TABLE_NEW_MENU_CATEGORY = "CREATE TABLE " + TABLE_MENU_ITEM_LIST + " ( ID INTEGER PRIMARY KEY,MenuItemName TEXT )";

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    db.execSQL(TABLE_MENU_ITEM_LIST);
}

public void addNewMenuInList(String menuCategoryName) {  // First enter you menu name in MenuItem List
    final SQLiteDatabase db = getWritableDatabase();
    final ContentValues contentValues = new ContentValues();
    contentValues.put(MENU_CATEGORY_NAME, menuCategoryName);
    int i = (int) db.insert(TABLE_MENU_ITEM_LIST, null, contentValues);
    db.close();
    //Now create new table for this category
    if (i > 0) {
        createNewMenuCategoryTable(menuCategoryName);
    }
}

public void createNewMenuCategoryTable(String menuItemName) {
    final SQLiteDatabase db = getWritableDatabase();
    // change your column datatype as per your requirement for price and coast
    String CREATE_TABLE_NEW_MENU_CATEGORY = "CREATE TABLE " + menuItemName + " ( ID INTEGER PRIMARY KEY,Name TEXT,Description TEXT,ImagePath TEXT,Price TEXT,Coast TEXT )";

    db.execSQL(CREATE_TABLE_NEW_MENU_CATEGORY);
    db.close();
}
enter code here