以下代码用于保存 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;
}
}
项目数据源:
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:没有这样的表:items:,编译时:INSERT INTO items(composition,menu_id,cooking_time,is_enabled,cost,description,name)VALUES(?,?,?,?, ?,?,?)"
但是如果我保存没有菜单的项目,那么一切都很好。我该如何修复它?谢谢。
最佳答案
请看这个answer ,您会发现不能让两个 SQLiteOpenHelper 类指向同一个数据库。
您可以将它们拆分为两个不同的数据库(通过为它们提供不同的数据库名称),但您可能想要做的是将两个 SQLiteOpenHelper 类合并为一个(以便您稍后可以运行使用这两个表的查询)。
关于java - Android SQLite 应用程序中没有这样的表(有 2 个表),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21557589/