Android:如何在数据库中检索我的欲望数据

时间:2013-02-25 07:32:35

标签: android database sqlite fetch

首先,我不知道我的数据库是否正常工作,因为我最终在here (it doesn't work also)更改了我之前的代码 现在我在newboston中使用sqlite的教程,但我仍然有一些错误它总是说不幸的是FC已经停止了。我没有我的代码中的问题,因为我没有编码错误。

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);
    }

});
}

}   

是我在数据库中输入/插入值的方式还是我的声明是错误的?

1 个答案:

答案 0 :(得分:0)

要获取logcat,您必须在logcat的左侧选择您的应用程序名称。请尝试粘贴错误。