Android Open Source - TacoTool D B Manager






From Project

Back to project page TacoTool.

License

The source code is released under:

GNU General Public License

If you think the Android project TacoTool listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package database;
/*from   ww  w .  j ava 2  s. c o m*/
import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteConstraintException;
import android.database.sqlite.SQLiteDatabase;

import com.example.tacotool.Taco;

public class DBManager {

  private SQLiteDatabase db;
  private DBSQLiteHelper myDB;

  public DBManager(Context context) {
    myDB = new DBSQLiteHelper(context);

  }

  public void open() throws SQLException {
    db = myDB.getWritableDatabase();
  }

  public void close() {
    myDB.close();
  }

  public boolean addIngredient(String ingredient) {

    try {
      ContentValues values = new ContentValues();
      values.put(myDB.COLUMN_INGREDIENT, ingredient);
      db.insertOrThrow(myDB.TABLE_INGREDIENTS, null, values);    
      
    } catch (Exception e) {
      return false;
    }
    return true;
  }

  public void clearTable(){
    myDB.clearTable(db);
  }



  public ArrayList<String> getIngredients() {
    ArrayList<String> ingredientList = new ArrayList<String>();
    String selectQuery = "SELECT  * FROM " + myDB.TABLE_INGREDIENTS;
    Cursor cursor = db.rawQuery(selectQuery, null);
    if (cursor.moveToFirst()) {
      do {
        ingredientList.add(cursor.getString(0));
      }
      while (cursor.moveToNext()); 
    }

    return ingredientList;
  }

  public boolean addRecipe(Taco taco){
    String query = "DELETE FROM " + myDB.TABLE_RECIPES + " WHERE recipeName =" + "\"" + taco.getName() + "\"";  
    db.execSQL(query);
    
    ContentValues row;
    for (String s : taco.getIngredients())  {
      row = new ContentValues();
      row.put(myDB.COLUMN_RECIPE, taco.getName());
      row.put(myDB.COLUMN_INGREDIENT, s);
      System.out.println("taconamn: " + taco.getName() + " ingrediens: " + s);
      db.insert(myDB.TABLE_RECIPES, null, row);
    }    
    return false;
  }
  
  public boolean removeRecipe(String rec) {
    String query = "DELETE FROM " + myDB.TABLE_RECIPES + " WHERE recipeName =" + "\"" + rec + "\"";  
    
    try {
      db.execSQL(query);
      return true;
    } catch (SQLException e) {
      return false;
    }
  }
  
  public boolean removeIngredient(String ingr){
    String query = "DELETE FROM " + myDB.TABLE_INGREDIENTS + " WHERE ingredient =" + "\"" + ingr + "\"";
    
    try {
      db.execSQL(query);
      return true;
    } catch (SQLException e) {
      return false;
    }
  }
  
  public ArrayList<String> getRecipes(){
    ArrayList<String> list = new ArrayList<String>();
    String query = "SELECT DISTINCT recipeName FROM " + myDB.TABLE_RECIPES;
    Cursor cursor = db.rawQuery(query, null);
    
    if (cursor.moveToFirst()) {
      do {
        list.add(cursor.getString(0));
      }
      while (cursor.moveToNext()); 
    }
    return list;
  }
  
  
  public Taco getRecipe(String recipe) {
    ArrayList<String> list = new ArrayList<String>();
    Taco taco = new Taco();
    String query = "SELECT * FROM " + myDB.TABLE_RECIPES + " WHERE recipeName=" + "\""+recipe+"\"";
    Cursor cursor = db.rawQuery(query,  null);
    if (cursor.moveToFirst()) {
      do {
        taco.addIngredient(cursor.getString(1));
        taco.setName(cursor.getString(0));
      }
      while (cursor.moveToNext()); 
    }
    return taco;
  }

}




Java Source Code List

com.example.tacotool.CreateActivity.java
com.example.tacotool.MainActivity.java
com.example.tacotool.ManageActivity.java
com.example.tacotool.SettingsActivity.java
com.example.tacotool.Taco.java
database.DBManager.java
database.DBSQLiteHelper.java