我想从数据库中获取一些数据。我没有收到任何错误消息。这是不起作用的代码:
数据库助手:
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);
}
});
}
}