Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
320 views
in Technique[技术] by (71.8m points)

Android : How can I retrieve my desire data in my Database

First of all I don't know if my database is working since I ended up changing my previous code in here (it doesn't work also) now I use the tutorials in newboston for sqlite, but I m still having some errors it always says unfortunately FC has stopped. I don't what is the problem in my code since I Didn't had an error coding it.

public class DatabaseHelper  {

    public static final String KEY_ROWID = "_id";
    public static final String KEY_NAME = "recipename";
    public static final String KEY_INGRDNAME = "ingrdname";

    public static final String MENU_ID = "_id";
    public static final String RECIPE_NAME = "recipe";


    private static final String MYDATABASE = "recipebook";
    private static final int VERSION = 1;

    private static final String RECIPE_TABLE = "recipe";
    private static final String MENU_TABLE = "menu";
    static final String viewrecipe ="viewrecipe";

    private DbHelper ourHelper;
    private final Context ourContext;
    private SQLiteDatabase ourDatabase;

    private static class DbHelper extends SQLiteOpenHelper{
     public DbHelper(Context context) {
            super(context, MYDATABASE, null, VERSION);
          }

     @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub


            db.execSQL("CREATE TABLE "+RECIPE_TABLE+" ("+KEY_ROWID+ " INTEGER PRIMARY KEY AUTOINCREMENT , "+
                    KEY_NAME+ " TEXT, " + KEY_INGRDNAME+ "TEXT );");

            db.execSQL("CREATE TABLE "+MENU_TABLE+" ("+MENU_ID+" INTEGER PRIMARY KEY, "+
                    RECIPE_NAME +" INTEGER NOT NULL ,FOREIGN KEY ("+RECIPE_NAME+") REFERENCES "+RECIPE_TABLE+" ("+KEY_ROWID+"));");




            db.execSQL("CREATE VIEW "+viewrecipe+
                    " AS SELECT "+RECIPE_TABLE +"."+KEY_NAME+""+
                    " FROM "+MENU_TABLE+" JOIN "+RECIPE_TABLE+
                    " ON "+MENU_TABLE+"."+RECIPE_NAME+" ="+RECIPE_TABLE+"."+KEY_ROWID
                    );

        }
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub

            db.execSQL("DROP TABLE IF EXISTS "+MENU_TABLE);
            db.execSQL("DROP TABLE IF EXISTS "+RECIPE_TABLE);
            db.execSQL("DROP VIEW IF EXISTS "+viewrecipe);
            onCreate(db);
        }

    }
    public DatabaseHelper (Context c){
        ourContext =c;
    }
    public DatabaseHelper open(){
        ourHelper = new DbHelper(ourContext);
        ourDatabase = ourHelper.getReadableDatabase();
        return this;
    }
    public void close(){
        ourHelper.close();
    }
   public  void InsertRecipe()
    {

    ContentValues values = new ContentValues();

    values.put(KEY_NAME, "Beef tapa");
    values.put(KEY_INGRDNAME, " 2 pounds beef round or chuck, sliced thinly against the grain , 1/2 cup lemon juice, 1/3 cup soy sauce , 2 tablespoons sugar, 1/4 teaspoon ground black pepper, 1 head garlic, minced, Canola or any cooking oil for frying " );
    ourDatabase.insert(RECIPE_TABLE, null, values);

    values.put(KEY_NAME, "Bulalo");
    values.put(KEY_INGRDNAME, " 6 - 7 pounds Beef Shanks with Bone Marrow, 1 head Garlic, cut, 1 large Onion, cubed, 2 Carrots, roughly cut, 2 stalks Celery, roughly cut, 1 teaspoon Whole Peppercorns, 1 tablespoon Salt, Leeks (optional), 2 - 3 Potatoes, cubed, 2 Corn on the cob, cut in 1 1/2 inch, 2 stalks Pechay or Bok Choy, 1 gallon Water, or enough to cover meat" );
    ourDatabase.insert(RECIPE_TABLE, null, values);

    values.put(KEY_NAME, "Caldereta");
    values.put(KEY_INGRDNAME, "2 pounds Beef, cut in 2 inch cubes, 1/2 head garlic, minced, 1/2 cup vinegar, 2 teaspoon salt, 2 pieces bay leaf (dahon ng laurel), enough water to cover the beef, 1/2 head garlic, minced, 1 medium onion, chopped, 1 small can tomato sauce, 6 small potatoes, quartered, 2-3 small Chili pepper, cut finely, 1 red bell pepper, cut in strips, 1/2 cup Green peas(optional), 1/2 cup Pickle Relish, 1 small can Liver Spread, 1/2 cup Olives, Salt and Pepper to taste, 2 teaspoon sugar, 2 tablespoons cooking oil, for sauteing, LIVER GRAVY INGREDIENTS (Optional), 1/2 pound beef liver, ground, 2 tablespoons vinegar, 2 tablespoons breadcrumbs, 1 cup beef broth, salt and pepper.");
    ourDatabase.insert(RECIPE_TABLE, null, values);

    values.put(KEY_NAME, "Kare kare");
    values.put(KEY_INGRDNAME, "2 pounds Oxtail, cut in 1 thickness,1 pound beef tripe (tuwalya), pressure cooked, cut into 2 squares (optional), 1/2 pound string beans, sliced in 3 lengths, 2 pcs. eggplants, sliced in 3 wedges, 1 banana bud or heart(puso ng saging), sliced in strips(optional), 6 stalks Bok Choy(Pechay), leaves and stalk separated, cut in 2 lengths, 1/2 cup peanut butter, dissolved in 1 cup beef broth, 1/4 cup toasted rice, powdered, dissolved in 1/2 cup beef broth, 5 cups beef broth, 2 tablespoons achuete seeds, 1/4 cup cooking oil,3 cloves garlic, minced, 1 medium onion, chopped, 1 tablespoon salt,1 tablespoon sugar, 1/4 teaspoon pepper, Sauteed Shrimp Paste");
    ourDatabase.insert(RECIPE_TABLE, null, values);


}
public String getIngredient(long l){
    String[]columns = new String[]{KEY_ROWID,KEY_NAME,KEY_INGRDNAME};
    Cursor c = ourDatabase.query(RECIPE_TABLE, columns, KEY_ROWID + "= " + l, null, null, null, null);
    if (c != null){
        c.moveToFirst();
        String ingredient = c.getString(2);
        return ingredient;
        }
    return null;
    }
    }


public class menucategory1a1 extends Activity {

        TextView text;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.beef1);


        TextView text = (TextView)findViewById(R.id.beeftapaingredients);
        String s = "1";
      long l = Long.parseLong(s);
      DatabaseHelper recipe = new DatabaseHelper(this);
      recipe.open();
      String ingrd = recipe.getIngredient(l);
      recipe.close();

      text.setText(ingrd);







    Button page1 = (Button) findViewById(R.id.button1);

    page1.setOnClickListener(new View.OnClickListener(){ 
    @Override
    public void onClick(View view) { 
    Intent myIntent = new Intent(view.getContext(), menucategory1a1a.class);
    startActivityForResult(myIntent, 0);
    }

});
}

}   

is it the way I input/Insert the values in database or my declaration is wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To get the logcat , you have to select your application name in the left side of logcat.Please try and paste the error.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...