数据库表未找到该列

时间:2014-05-09 20:06:28

标签: android sqlite database-design

这是我主要活动中的创建数据:

private void createData() {

    //List<Mainfoodsparcelable> mainfoodsparcelables = parser.parseXML(this);
    Mainfoodsparcelable mainfoodsparcelable = new Mainfoodsparcelable();
    mainfoodsparcelable.setFoodname("its a food name");
    mainfoodsparcelable.setMaterials("very much indeed");
    mainfoodsparcelable.setFooddetails("now cook it");
    mainfoodsparcelable.setLikes(1);
    mainfoodsparcelable.setDislikes(2);
    mainfoodsparcelable.setReports(3);
    mainfoodsparcelable.setComments("awesome");
    mainfoodsparcelable.setImage("salton_sea");
    mainfoodsparcelable = datasource.create(mainfoodsparcelable);
    //for (Mainfoodsparcelable mainfoodsparcelable : mainfoodsparcelables) {
    //mainfoodsparcelable   = datasource.create(mainfoodsparcelable);
    Log.i(LOGTAG,"Food created with id " + mainfoodsparcelable.getFoodid());
    //here gets an id from the getFoodid method in the other class
}

然后,这是我的parcelable类,这个方法指向set ***

public class Mainfoodsparcelable implements Parcelable{
private long foodid;
private String foodname;
private String materials;
private double likes;
private double dislikes;
private double reports;
private String fooddetails;
private String image;
private String comments;

public long getFoodid() {
    return foodid;
}
public void setFoodid(long foodid) {
    this.foodid = foodid;
}
public String getFoodname() {
    return foodname;
}
public void setFoodname(String foodname) {
    this.foodname = foodname;
}
public String getMaterials() {
    return materials;
}
public void setMaterials(String materials) {
    this.materials = materials;
}
public double getLikes() {
    return likes;
}
public void setLikes(double likes) {
    this.likes = likes;
}

public double getDislikes() {
    return dislikes;
}
public void setDislikes(double dislikes) {
    this.dislikes = dislikes;
}

public double getReports() {
    return reports;
}
public void setReports(double reports) {
    this.reports = reports;
}

public String getFooddetails() {
    return fooddetails;
}
public void setFooddetails(String fooddetails) {
    this.fooddetails = fooddetails;
}
public String getImage() {
    return image;
}
public void setImage(String image) {
    this.image = image;
}
public String getComments() {
    return comments;
}
public void setComments(String comments) {
    this.comments = comments;
}
@Override
public String toString() {
    //NumberFormat nf = NumberFormat.getCurrencyInstance();
    return foodname ;
            //+ "\n(" + nf.format(price) + ")";
}
public Mainfoodsparcelable() {
}

public Mainfoodsparcelable(Parcel in) {
     Log.i(MainActivity.LOGTAG, "Parcel constructor");
    //parcel named in
     foodid = in.readLong();
     foodname = in.readString();
     materials = in.readString();
     likes = in.readDouble();
     dislikes = in.readDouble();
     reports = in.readDouble();
     fooddetails = in.readString();
     image = in.readString();
     comments = in.readString();
}

@Override
public int describeContents() {
     return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
     Log.i(MainActivity.LOGTAG, "writeToParcel");

     dest.writeLong(foodid);
     dest.writeString(foodname);
     dest.writeString(materials);
     dest.writeDouble(likes);
     dest.writeDouble(dislikes);
     dest.writeDouble(reports);
     dest.writeString(image);
     dest.writeString(comments);
}

public static final Parcelable.Creator<Mainfoodsparcelable> CREATOR =
          new Parcelable.Creator<Mainfoodsparcelable>() {

     @Override
     public Mainfoodsparcelable createFromParcel(Parcel source) {
          return new Mainfoodsparcelable(source);
     }

     @Override
     public Mainfoodsparcelable[] newArray(int size) {

          return new Mainfoodsparcelable[size];
     }

};

这是我需要的openhelper类,我不这么认为:

public class FoodsbymainDBOpenHelper extends SQLiteOpenHelper {

private static final String LOGTAG = "LIFEMATE";
private static final String DATABASE_NAME = "foods.db";
private static final int DATABASE_VERSION = 1;

public static final String TABLE_MAINFOODSLIST = "mainfoodslist";
public static final String COLUMN_FOODID = "foodid";
public static final String COLUMN_FOODNAME = "foodname";
public static final String COLUMN_FOODMATERIALS = "materials";
public static final String COLUMN_FOODLIKES = "likes";
public static final String COLUMN_FOODDISLIKES = "dislikes";
public static final String COLUMN_FOODREPORTS = "reports";
public static final String COLUMN_FOODDETAILS = "fooddetails";
public static final String COLUMN_FOODIMAGE = "image";
public static final String COLUMN_FOODCOMMENTS = "comments";

private static final String TABLE_CREATE = 
        "CREATE TABLE " + TABLE_MAINFOODSLIST+ " (" +
        COLUMN_FOODID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
        COLUMN_FOODNAME + " TEXT, " +
        COLUMN_FOODMATERIALS + " TEXT, " +
        COLUMN_FOODLIKES + " NUMERIC " +
        COLUMN_FOODDISLIKES + " NUMERIC " +
        COLUMN_FOODREPORTS + " NUMERIC " +
        COLUMN_FOODDETAILS + " TEXT, " +
        COLUMN_FOODIMAGE + " IMAGE " +
        COLUMN_FOODCOMMENTS + " TEXT " +
        ")";



public FoodsbymainDBOpenHelper(Context context) {
    super(context, DATABASE_NAME , null, DATABASE_VERSION);
    //This is constructor method, and will be called ONLY by your own code
    //store the context for later use
    //this is how the this class will be connected to the current activity
}

@Override
public void onCreate(SQLiteDatabase db) {
    //SQLitedatabase named db and executed below
    //The constructor does two things. First, it checks whether the database exists and, if not, will call the onCreate method. Second, if the database does exist, it will check whether the existing database version number differs from the one implemented in the constructor, so as to determine if the database has been updated. If it has, the onUpgrade method will be called.
    // Does the database exist? if not creat it
    db.execSQL(TABLE_CREATE);
    // TABLE_CREATE points at the details on top of this page
    // now the table is created
    Log.i(LOGTAG,"Table has been created");


}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // has database version upgraded? then upgrade it
    db.execSQL("DROP TABLE IF EXISTS" + TABLE_MAINFOODSLIST);
    //the blue part is a sql command , if the table exists, it will delete it and so the oncreate method will be called !
    onCreate(db);
    //so db has recreated any way !
}

}

运行应用程序后,我在logCat中收到此错误:

I/LIFEMATE(1425): Database opened
I/LIFEMATE(1425): Table has been created
E/SQLiteLog(1425): (1) table mainfoodslist has no column named reports
E/SQLiteDatabase(1425): Error inserting reports=3.0 materials=very much              
fooddetails=now cook it likes=1.0 image=salton_sea dislikes=2.0 foodname=its a food 
name comments=awesome
E/SQLiteDatabase(1425): android.database.sqlite.SQLiteException: table mainfoodslist   
has no column named reports (code 1): , while compiling: INSERT INTO 
mainfoodslist(reports,materials,fooddetails,likes,image,dislikes,foodname,comments) 
VALUES (?,?,?,?,?,?,?,?)
E/SQLiteDatabase(1425):     at 
android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)

在表格中说,我没有一个名为报告的专栏!!我已经在那里搜索了所有内容,但我没有发现任何问题,你能帮我一些吗?非常感谢

0 个答案:

没有答案