以下代码用于保存Menu和Item对象:
public void setMenus(List<Menu> menus) {
mMenuDataSource.saveMenus(menus);
for (Menu menu:menus) {
mItemDataSource.saveItems(menu.getItems(), menu.getId());
}
}
MenuDataSource类:
public class MenuDataSource {
private SQLiteDatabase mDatabase;
private MenuSQLiteHelper mHelper;
private String[] mAllColumns = { MenuSQLiteHelper.COLUMN_ID,
MenuSQLiteHelper.COLUMN_NAME, MenuSQLiteHelper.COLUMN_IS_ENABLED };
private ItemDataSource mItemDataSource;
public MenuDataSource(Context context) {
mHelper = new MenuSQLiteHelper(context);
}
public void saveMenus(List<Menu> menus) {
mDatabase = mHelper.getWritableDatabase();
for (Menu menu : menus) {
long menuId = mDatabase.insert(MenuSQLiteHelper.TABLE_NAME, null,
menuToContentValues(menu));
menu.setId(menuId);
}
}
private ContentValues menuToContentValues(Menu menu) {
ContentValues values = new ContentValues();
values.put(MenuSQLiteHelper.COLUMN_NAME, menu.getName());
values.put(MenuSQLiteHelper.COLUMN_IS_ENABLED, menu.isEnabled());
return values;
}
}
ItemDataSource:
public class ItemDataSource {
private SQLiteDatabase mDatabase;
private ItemSQLiteHelper mHelper;
private String[] mAllColumns = { ItemSQLiteHelper.COLUMN_ID,
ItemSQLiteHelper.COLUMN_NAME, ItemSQLiteHelper.COLUMN_IS_ENABLED,
ItemSQLiteHelper.COLUMN_COST, ItemSQLiteHelper.COLUMN_COMPOSITION,
ItemSQLiteHelper.COLUMN_COOKING_TIME, ItemSQLiteHelper.COLUMN_MENU_ID,
ItemSQLiteHelper.COLUMN_DESCRIPTION, ItemSQLiteHelper.COLUMN_COMPOSITION };
public ItemDataSource(Context context) {
mHelper = new ItemSQLiteHelper(context);
}
public void saveItems(List<Item> items, long menuId) {
mDatabase = mHelper.getWritableDatabase();
for (Item item : items) {
long itemId = mDatabase.insert(ItemSQLiteHelper.TABLE_NAME, null, itemToContentValues(item, menuId));
Log.e("item_id", String.valueOf(itemId));
}
mDatabase.close();
}
private ContentValues itemToContentValues(Item item, long menuId) {
ContentValues values = new ContentValues();
values.put(ItemSQLiteHelper.COLUMN_NAME, item.getName());
values.put(ItemSQLiteHelper.COLUMN_DESCRIPTION, item.getDescription());
values.put(ItemSQLiteHelper.COLUMN_COMPOSITION, item.getComposition());
values.put(ItemSQLiteHelper.COLUMN_COST, item.getCost());
values.put(ItemSQLiteHelper.COLUMN_IS_ENABLED, item.isEnabled());
values.put(ItemSQLiteHelper.COLUMN_COOKING_TIME, item.getCookingTime());
values.put(ItemSQLiteHelper.COLUMN_MENU_ID, menuId);
return values;
}
}
MenuSQLiteHelper类:
public class MenuSQLiteHelper extends SQLiteOpenHelper {
public static final String TABLE_NAME = "menus";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_NAME = "name";
public static final String COLUMN_IS_ENABLED = "is_enabled";
private static final String DATABASE_CREATE = "create table "
+ TABLE_NAME + "(" + COLUMN_ID + " integer primary key autoincrement, " +
COLUMN_NAME + " text, " +
COLUMN_IS_ENABLED + " integer);";
public MenuSQLiteHelper(Context context) {
super(context, DbConstants.DATABASE_NAME, null, DbConstants.DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(DATABASE_CREATE);
Log.e("menu", "created");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.e("menu", "updated");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
ItemSQLiteHelper类:
public class ItemSQLiteHelper extends SQLiteOpenHelper {
public static final String TABLE_NAME = "items";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_NAME = "name";
public static final String COLUMN_DESCRIPTION = "description";
public static final String COLUMN_COMPOSITION = "composition";
public static final String COLUMN_COST = "cost";
public static final String COLUMN_COOKING_TIME = "cooking_time";
public static final String COLUMN_MENU_ID = "menu_id";
public static final String COLUMN_IS_ENABLED = "is_enabled";
private static final String DATABASE_CREATE = "create table " +
TABLE_NAME + "(" + COLUMN_ID + " integer primary key autoincrement, " +
COLUMN_NAME + " text, " +
COLUMN_DESCRIPTION + " text, " +
COLUMN_COMPOSITION + " text, " +
COLUMN_COST + " integer, " +
COLUMN_COOKING_TIME + " integer, " +
COLUMN_MENU_ID + " integer, " +
COLUMN_IS_ENABLED + " integer);";
public ItemSQLiteHelper(Context context) {
super(context, DbConstants.DATABASE_NAME, null, DbConstants.DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database) {
Log.e("item", "created");
database.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.e("item", "updated");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
正如您所看到的,代码非常简单 - 它只创建了2个表并保存了菜单和项目。但是我收到以下错误:
"android.database.sqlite.SQLiteException: no such table: items: , while compiling: INSERT INTO items(composition,menu_id,cooking_time,is_enabled,cost,description,name) VALUES (?,?,?,?,?,?,?)"
但是,如果我保存没有菜单的项目,那么一切都很好。我该如何解决?谢谢。
答案 0 :(得分:1)
请查看此answer,您会发现您不能将两个SQLiteOpenHelper类指向同一个数据库。
您可以将它们分成两个不同的数据库(通过为它们提供不同的数据库名称),但您可能要做的是将两个SQLiteOpenHelper类合并为一个(这样您以后可以运行使用这两个表的查询)。 / p>
答案 1 :(得分:1)
一开始没有数据库文件。
当您首先运行mMenuDataSource
的方法时,它会发生“oncreate”,并使用表“menus”创建数据库;
然后运行mItemDataSource
的方法,但是已经创建了数据库文件,所以它的“oncreate”没有发生,因此没有任何东西可以创建“items”表。
您应该在相同的“oncreate”方法中创建两个表。