安卓:我怎样才能找回我的资料库我的愿望数据 [英] Android : How can I retrieve my desire data in my Database

查看:163
本文介绍了安卓:我怎样才能找回我的资料库我的愿望数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先我不知道我的数据库工作,因为我最终改变我的previous code在<一个href=\"http://stackoverflow.com/questions/15049544/android-how-can-i-fetch-my-selected-id-in-my-sqlite\">here (它不也行)
现在我用的教程newboston SQLite的,但我米仍然有一些错误,它总是说不幸的是FC已经停止。我不知道是什么问题在我的code,因为我没有出现错误编码它。

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?

推荐答案

要获得logcat中,你有logcat.Please左边选择你的应用程序的名字尝试,并粘贴错误。

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

这篇关于安卓:我怎样才能找回我的资料库我的愿望数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆