Untitled
unknown
plain_text
20 days ago
5.7 kB
7
Indexable
package com.nonigopal.ajkerkhobor; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class DatabaseHelper extends SQLiteOpenHelper { private static final String DATABASE_NAME = "FoodMenu.db"; private static final int DATABASE_VERSION = 1; // Table names private static final String TABLE_GROUPS = "groups"; private static final String TABLE_ITEMS = "items"; // Group Table Columns private static final String COLUMN_GROUP_ID = "id"; private static final String COLUMN_GROUP_NAME = "name"; private static final String COLUMN_GROUP_IMAGE = "image"; // Item Table Columns private static final String COLUMN_ITEM_ID = "id"; private static final String COLUMN_ITEM_NAME = "name"; private static final String COLUMN_ITEM_IMAGE = "image"; private static final String COLUMN_ITEM_PRICE = "price"; private static final String COLUMN_ITEM_GROUP_ID = "group_id"; public DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { // Create Groups Table String createGroupsTable = "CREATE TABLE " + TABLE_GROUPS + " (" + COLUMN_GROUP_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_GROUP_NAME + " TEXT, " + COLUMN_GROUP_IMAGE + " TEXT)"; // Create Items Table String createItemsTable = "CREATE TABLE " + TABLE_ITEMS + " (" + COLUMN_ITEM_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_ITEM_NAME + " TEXT, " + COLUMN_ITEM_IMAGE + " TEXT, " + COLUMN_ITEM_PRICE + " REAL, " + COLUMN_ITEM_GROUP_ID + " INTEGER, " + "FOREIGN KEY(" + COLUMN_ITEM_GROUP_ID + ") REFERENCES " + TABLE_GROUPS + "(" + COLUMN_GROUP_ID + "))"; db.execSQL(createGroupsTable); db.execSQL(createItemsTable); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS " + TABLE_GROUPS); db.execSQL("DROP TABLE IF EXISTS " + TABLE_ITEMS); onCreate(db); } // --- Group Operations --- // Insert New Group public boolean insertGroup(String name, String imagePath) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(COLUMN_GROUP_NAME, name); values.put(COLUMN_GROUP_IMAGE, imagePath); long result = db.insert(TABLE_GROUPS, null, values); return result != -1; // Returns true if insert is successful } // Get All Groups public Cursor getAllGroups() { SQLiteDatabase db = this.getReadableDatabase(); return db.rawQuery("SELECT * FROM " + TABLE_GROUPS, null); } // Delete Group (and all items in that group) public boolean deleteGroup(int groupId) { SQLiteDatabase db = this.getWritableDatabase(); db.delete(TABLE_ITEMS, COLUMN_ITEM_GROUP_ID + "=?", new String[]{String.valueOf(groupId)}); int deletedRows = db.delete(TABLE_GROUPS, COLUMN_GROUP_ID + "=?", new String[]{String.valueOf(groupId)}); return deletedRows > 0; } // Update Group Name & Image public boolean updateGroup(int groupId, String newName, String newImage) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(COLUMN_GROUP_NAME, newName); values.put(COLUMN_GROUP_IMAGE, newImage); int updatedRows = db.update(TABLE_GROUPS, values, COLUMN_GROUP_ID + "=?", new String[]{String.valueOf(groupId)}); return updatedRows > 0; } // --- Item Operations --- // Insert New Item public boolean insertItem(String name, String imagePath, double price, int groupId) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(COLUMN_ITEM_NAME, name); values.put(COLUMN_ITEM_IMAGE, imagePath); values.put(COLUMN_ITEM_PRICE, price); values.put(COLUMN_ITEM_GROUP_ID, groupId); long result = db.insert(TABLE_ITEMS, null, values); return result != -1; } // Get All Items in a Group public Cursor getItemsByGroup(int groupId) { SQLiteDatabase db = this.getReadableDatabase(); return db.rawQuery("SELECT * FROM " + TABLE_ITEMS + " WHERE " + COLUMN_ITEM_GROUP_ID + "=?", new String[]{String.valueOf(groupId)}); } // Delete Item public boolean deleteItem(int itemId) { SQLiteDatabase db = this.getWritableDatabase(); int deletedRows = db.delete(TABLE_ITEMS, COLUMN_ITEM_ID + "=?", new String[]{String.valueOf(itemId)}); return deletedRows > 0; } // Update Item Details public boolean updateItem(int itemId, String newName, String newImage, double newPrice) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(COLUMN_ITEM_NAME, newName); values.put(COLUMN_ITEM_IMAGE, newImage); values.put(COLUMN_ITEM_PRICE, newPrice); int updatedRows = db.update(TABLE_ITEMS, values, COLUMN_ITEM_ID + "=?", new String[]{String.valueOf(itemId)}); return updatedRows > 0; } }
Editor is loading...
Leave a Comment