2013-02-24 54 views
1

我想從數據庫中獲取一些數據。我沒有收到任何錯誤消息。下面是不起作用的代碼:android:我如何在我的sqlite中獲取我選擇的id

數據庫幫手:

import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 

public class DatabaseHelper extends SQLiteOpenHelper { 

    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.db"; 
    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"; 

    public DatabaseHelper(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 
     ); 
     //Inserts pre-defined departments 
     InsertRecipe(db); 

    } 
    @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); 
    } 
    void InsertRecipe(SQLiteDatabase db) {   
     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 "); 
     db.insert(RECIPE_TABLE, KEY_NAME, 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"); 
     db.insert(RECIPE_TABLE, KEY_NAME, 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."); 
     db.insert(RECIPE_TABLE, KEY_NAME, 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"); 
     db.insert(RECIPE_TABLE, KEY_NAME, values); 

     values.put(KEY_NAME, "Mechado");  
     values.put(KEY_INGRDNAME, " 2 pounds Top or Bottom Round Beef Roast, cut in 2-3 inch tubes, 1/2 pound Pork Fat, cut in strips, 5-6 Potatoes, cubed, 4 cloves Garlic, minced, 1 medium Onion, chopped, 1/3 cup Vinegar, 1 - 2 cups water, 2 tablespoons Soy Sauce, 1 small can tomato sauce, 1 pc. Bay Leaf, Salt and Pepper,3 tablespoons cooking oil"); 
     db.insert(RECIPE_TABLE, KEY_NAME, values); 
    } 


    public String getRecipe(int id) { 
    SQLiteDatabase db = this.getReadableDatabase(); 

     Cursor c = db.query(RECIPE_TABLE, new String[] { KEY_ROWID, 
        KEY_NAME, KEY_ROWID }, KEY_ROWID + "=?", 
        new String[] { String.valueOf(id) }, null, null, null, null); 
     if (c != null) 
      c.moveToFirst(); 

     String contact = new String(c.getString(id)); 
      // return contact 
     return contact; 
    } 

我的Java類。我希望它在textview獲取數據:

import android.app.Activity; 
import android.content.Intent; 


import android.os.Bundle; 

import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 



public class menucategory1a1 extends Activity { 

    private DatabaseHelper db; 

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

     TextView text = (TextView)findViewById(R.id.beeftapaingredients); 
    text.setText(db.getRecipe(0)); 

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

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

     }); 
    } 

} 
+0

您是否檢查數據庫以查看其中是否有數據? – JoxTraex 2013-02-24 07:16:03

+0

我還沒有檢查,因爲這是我第一次在android中創建數據庫.....我不知道我在哪裏可以看到我的數據庫,我只是按照我搜索的所有教程,這是它的結果,悲傷是史詩般的失敗。 – beginner 2013-02-24 10:06:32

回答

0

你的問題是在這裏String contact = new String(c.getString(id));的的getString(ID)採用列索引,試圖挑行不是ID。

+0

我試圖把\t \t菜單聯繫人=新菜單(Integer.parseInt(c.getString(0)), \t \t c.getString(1),c.getString(2)); \t \t \t //返回聯繫人 \t \t \t返回聯繫人; (和CharSequence)db.getRecipe(0));並再次運行它,然後再次運行它綠色調試宣傳TextView文本=(TextView)findViewById(R.id.beeftaparedredients);) – beginner 2013-02-24 08:13:25

+0

另一個問題是你的致電。您正在通過rowid進行搜索,硬編碼rowid來自您的活動。我不確定這是一個有效的價值 - rowids可能從1開始,或者我知道的所有開始點都是隨機的。我不會假設他們從0開始。 – 2013-02-24 08:20:10

+0

美好的一天先生,我已經更改了我的代碼,但是我仍然有錯誤,您能否再次檢查我的代碼中的錯誤?這裏是鏈接先生,http://stackoverflow.com/questions/15062062/android-how-can-i-retrieve-my-desire-data-in-my-database希望你再次謝謝我的幫助 – beginner 2013-02-25 07:34:58

相關問題