0

所以基本上代码工作正常,直到我点击我评论的行“//这不起作用!!!”。然后什么都没有输入到数据库中。任何人都知道为什么我可以为“StringCategory_Table”输入它们,但不能输入其他的?

public class SetupOfSQLite{


    private DBHelper myHelper;
    private final Context myContext;
    private SQLiteDatabase myDatabase;

     private static final String DATABASE_NAME = "restaurantDatabase";
     private static final int DATABASE_VERSION = 1;
     private static final String COLUMN_RESTAURANT = "RestaurantName";
     private static final String COLUMN_CATEGORIES= "CategoryName";

     public static String currentRestaurant = null;
     public static String deletingRestaurant= null;

     public static final String American_Table ="Restaurant_List_American";
     public static final String Asian_Table = "Restaurant_List_Asian";
     public static final String BarPub_Table = "Restaurant_List_BarPub";
     public static final String Breakfast_Table ="Restaurant_List_Breakfast";
     public static final String FastFood_Table ="Restaurant_List_FastFood";
     public static final String Healthy_Table = "Restaurant_List_Healthy";
     public static final String Mexican_Table ="Restaurant_List_Mexican";
     public static final String SeaFood_Table = "Restaurant_List_SeaFood";
     public static final String Italian_Table = "Restaurant_List_Italian";
     public static final String Deserts_Table = "Restaurant_List_Desert";
     public static final String StringCategory_Table = "Categories_of_Restaurants";

     static ContentValues contValues = new ContentValues();

private static class DBHelper extends SQLiteOpenHelper {

public DBHelper (Context ctx){
     super (ctx, DATABASE_NAME, null, DATABASE_VERSION);
     }

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE " + American_Table       + " (" + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_RESTAURANT + " TEXT NOT NULL);");  
    db.execSQL("CREATE TABLE " + Asian_Table          + " (" + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_RESTAURANT + " TEXT NOT NULL);");
    db.execSQL("CREATE TABLE " + BarPub_Table         + " (" + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_RESTAURANT + " TEXT NOT NULL);");
    db.execSQL("CREATE TABLE " + Breakfast_Table      + " (" + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_RESTAURANT + " TEXT NOT NULL);");
    db.execSQL("CREATE TABLE " + Deserts_Table        + " (" + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_RESTAURANT + " TEXT NOT NULL);");
    db.execSQL("CREATE TABLE " + FastFood_Table       + " (" + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_RESTAURANT + " TEXT NOT NULL);");
    db.execSQL("CREATE TABLE " + Healthy_Table        + " (" + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_RESTAURANT + " TEXT NOT NULL);");
    db.execSQL("CREATE TABLE " + Italian_Table        + " (" + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_RESTAURANT + " TEXT NOT NULL);");
    db.execSQL("CREATE TABLE " + Mexican_Table        + " (" + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_RESTAURANT + " TEXT NOT NULL);");
    db.execSQL("CREATE TABLE " + SeaFood_Table        + " (" + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_RESTAURANT + " TEXT NOT NULL);");
    // The table below is for keeping the categories persistent
    db.execSQL("CREATE TABLE " + StringCategory_Table + " (" + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COLUMN_CATEGORIES + " TEXT NOT NULL);");
    // The table below is for suggestions

    // now I'll create some entries for default categories
    contValues.put(COLUMN_CATEGORIES, "American"); 
    db.insert(StringCategory_Table, COLUMN_CATEGORIES, contValues);

    contValues.put(COLUMN_CATEGORIES, "Asian");
    db.insert(StringCategory_Table, COLUMN_CATEGORIES, contValues);

    contValues.put(COLUMN_CATEGORIES, "Bar or Pub");
    db.insert(StringCategory_Table, COLUMN_CATEGORIES, contValues);

    contValues.put(COLUMN_CATEGORIES, "Breakfast");
    db.insert(StringCategory_Table, COLUMN_CATEGORIES, contValues);

    contValues.put(COLUMN_CATEGORIES, "Deserts");
    db.insert(StringCategory_Table, COLUMN_CATEGORIES, contValues);

    contValues.put(COLUMN_CATEGORIES, "FastFood");
    db.insert(StringCategory_Table, COLUMN_CATEGORIES, contValues);

    contValues.put(COLUMN_CATEGORIES, "Healthy"); // this represents "Healthy" category
    db.insert(StringCategory_Table, COLUMN_CATEGORIES, contValues);

    contValues.put(COLUMN_CATEGORIES, "Italian");
    db.insert(StringCategory_Table, COLUMN_CATEGORIES, contValues);

    contValues.put(COLUMN_CATEGORIES, "Mexican");
    db.insert(StringCategory_Table, COLUMN_CATEGORIES, contValues);

    contValues.put(COLUMN_CATEGORIES, "SeaFood");
    db.insert(StringCategory_Table, COLUMN_CATEGORIES, contValues);

// now ill add in some restaurants for the first time the user installs the app 
//THIS DOESNT WORK!!!

    contValues.put(COLUMN_RESTAURANT, "Olive Guarden"); 
    db.insert(Italian_Table, COLUMN_RESTAURANT, contValues);

    contValues.put(COLUMN_RESTAURANT, "Red Lobster"); 
    db.insert(SeaFood_Table, COLUMN_RESTAURANT, contValues);

    contValues.put(COLUMN_RESTAURANT, "Outback"); 
    db.insert(American_Table, COLUMN_RESTAURANT, contValues);

    contValues.put(COLUMN_RESTAURANT, "Dunkin' Donuts"); 
    db.insert(Breakfast_Table, COLUMN_RESTAURANT, contValues);

    contValues.put(COLUMN_RESTAURANT, "Taco Bell"); 
    db.insert(Mexican_Table, COLUMN_RESTAURANT, contValues);

    contValues.put(COLUMN_RESTAURANT, "Burger King"); 
    db.insert(FastFood_Table, COLUMN_RESTAURANT, contValues);  //1st entry for fast food

    contValues.put(COLUMN_RESTAURANT, "McDonald's"); 
    db.insert(FastFood_Table, COLUMN_RESTAURANT, contValues);  // 2 entries for fast food

    contValues.put(COLUMN_RESTAURANT, "Cold Stone"); 
    db.insert(Deserts_Table, COLUMN_RESTAURANT, contValues);

    contValues.put(COLUMN_RESTAURANT, "Panda"); 
    db.insert(Asian_Table, COLUMN_RESTAURANT, contValues);

    contValues.put(COLUMN_RESTAURANT, "Subway"); 
    db.insert(Healthy_Table, COLUMN_RESTAURANT, contValues);

}
4

1 回答 1

3

我认为您的contValues变量具有陈旧的列信息,这对餐厅餐桌没有意义。尝试以下操作:

contValues.clear();
contValues.put(COLUMN_RESTAURANT, "Olive Guarden"); 
于 2012-12-07T21:10:49.460 回答