2016-10-07 5 views

答えて

1

SQLiteデータベースに必要なCRUD関数を使用してDatabaseAdapterクラスを作成する必要があります。まず、DatabaseAdapter.javaという新しいJavaクラスファイルを作成します。 、今

public class DatabaseAdapter { 
    private static final String databaseName = "MealsDatabase"; 
    private static final int databaseVersion = 1; 
    private static final String databaseTable = "Meals"; 
    private static final String columnId = "id"; 
    private static final String columnQuantity = "quantity"; 
    private static final String columnMeal = "meal"; 
    private static final String columnPrice = "price"; 
    private static final String columnAmount = "amount"; 

    DatabaseHelper helper; 
    SQLiteDatabase database; 

    public DatabaseAdapter(Context context) { 
     this.helper = new DatabaseHelper(context, databaseName, databaseVersion); 
    } 

    public DatabaseAdapter open() throws SQLException { 
     this.database = helper.getWritableDatabase(); 
     return this; 
    } 

    public void close() throws SQLException { 
     this.helper.close(); 
    } 

    public Cursor getAllMeals() { 
     return this.database.query(databaseTable, new String[] {columnId, columnName, columnDate, columnDescription}, null, null, null, null, null); 
    } 

    public Cursor getMeal(long id) throws SQLException { 
     Cursor cursor = this.database.query(true, databaseTable, new String[] {columnId, columnQuantity, columnMeal, columnPrice, columnAmount}, columnId + "=" + id, null, null, null, null, null); 

     if (cursor != null) { 
      cursor.moveToFirst(); 
     } 
     return cursor; 
    } 

    public long insertMeal(int quantity, String meal, float price, int amount) { 
     ContentValues contentValues = new ContentValues(); 

     contentValues.put(columnQuantity, quantity); 
     contentValues.put(columnMeal, meal); 
     contentValues.put(columnPrice, price); 
     contentValues.put(columnAmount, amount); 

     return this.database.insert(databaseTable, null, contentValues); 
    } 

    public boolean editMeal(long id, int quantity, String meal, float price, int amount) { 
     ContentValues contentValues = new ContentValues(); 

     contentValues.put(columnQuantity, quantity); 
     contentValues.put(columnMeal, meal); 
     contentValues.put(columnPrice, price); 
     contentValues.put(columnAmount, amount); 

     return this.database.update(databaseTable, contentValues, columnId + "=" + id, null) > 0; 
    } 

    public boolean deleteMeal(long id) { 
     return this.database.delete(databaseTable, columnId + "=" + id, null) > 0; 
    } 
} 

DatabaseHelper.javaと呼ばれる新しいJavaクラスを作成し、次のコードを追加します。次のコードを挿入し、新たな食事を作成するアクティビティで

public class DatabaseHelper extends SQLiteOpenHelper { 
    DatabaseHelper(Context context, String databaseName, int databaseVersion) { 
     super(context, databaseName, null, databaseVersion); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase database) { 
     try { 
      database.execSQL("CREATE TABLE Meals (id INTEGER PRIMARY KEY AUTOINCREMENT, quantity INTEGER NOT NULL, meal TEXT NOT NULL, price REAL NOT NULL, amount INTEGER NOT NULL)"); 
     } catch (SQLException exception) { 
      exception.printStackTrace(); 
     } 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) { 
     Log.w("MealsDatabase", "Upgrading database from version " + oldVersion + " to " + newVersion + "."); 
     database.execSQL("DROP TABLE IF EXISTS SavedLists"); 
     onCreate(database); 
    } 
} 

を、次のコードを追加します。

DatabaseAdapter database = new DatabaseAdapter(this); 
database.open(); 

if (database.insertMeal(quantity, "Meal", 5.60, amount) > 0) { 
    Toast.makeText(this, "A new meal has been created!", Toast.LENGTH_SHORT).show(); 
} 

database.close(); 
は、既存のレコードを更新するには、次のコードを追加します

食事データベースを作成し、食事を追加し、既存の食事を更新しました。

+0

ありがとうございます:) – jardin

関連する問題