在Sqlite数据库Android中插入时出错

时间:2013-01-29 20:08:17

标签: android android-sqlite

我被困住了。一直在寻找合理解决方案的时间,但没有任何线索。

这是我的创作代码:

  public class EventData extends SQLiteOpenHelper {

  public static final String TABLE_COMMENTS = "EventData";          //Our Table Name(let say ) Event Data

  public static final String TABLE_EVENT_NAME="eventName";
  public static final String TABLE_DAY="day";
  public static final String TABLE_START="sTime";
  public static final String TABLE_END="eTime";
  public static final String TABLE_IMGID="imgId";   //Primary Key
  public static final String TABLE_SUBSCRIBE="subStatus";
  //ImgId,sTime,eTime is int type(rest all are of String type)
  public static final String TABLE_FILENAME="fileName";

  private static final String DATABASE_NAME = "EventDB.db";
  private static final int DATABASE_VERSION = 1;

  // Database creation sql statement
  public static final String DATABASE_CREATE = "create table "
      + TABLE_COMMENTS + "(" + TABLE_IMGID
      + " integer primary key , " + TABLE_EVENT_NAME
      + " text , " +    TABLE_DAY+
      " text ," +       TABLE_START+
      " float , " + TABLE_END+
      " float , " +     TABLE_SUBSCRIBE+
      " integer ," +    TABLE_FILENAME+
      " text );";


  public EventData(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
  }

  @Override
  public void onCreate(SQLiteDatabase database) {
    database.execSQL(DATABASE_CREATE);
  }

  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    Log.w(EventData.class.getName(),
        "Upgrading database from version " + oldVersion + " to "
            + newVersion + ", which will destroy all old data");
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_COMMENTS);
    onCreate(db);
  }   
} 

这是我在其他课程中的插入代码:

 public Comment createComment(int id,String eN,String d,int sT,int eT,String fN,Boolean sub) {
ContentValues values = new ContentValues();
 //  values.put(EventData.TABLE_EVENT_NAME, comment);
values.put(EventData.TABLE_IMGID, id);
values.put(EventData.TABLE_EVENT_NAME,eN);
values.put(EventData.TABLE_DAY, d);
values.put(EventData.TABLE_START, sT);
values.put(EventData.TABLE_END, eT);
int temp=0;
if(sub==true)temp=1;
values.put(EventData.TABLE_SUBSCRIBE,temp);
values.put(EventData.TABLE_FILENAME, fN);


long insertId = database.insert(EventData.TABLE_COMMENTS, null,
    values);

Log.d("DB", "value inserted succcessfully");

Cursor cursor = database.query(EventData.TABLE_COMMENTS,allColumns, EventData.TABLE_IMGID + " = " + insertId, null,null, null, null);
Log.d("Sucess", "good so far");  
cursor.moveToFirst();
Comment newComment = cursorToComment(cursor);
cursor.close();
return newComment;
}

这里是logcat

01-30 01:31:53.141: E/Database(8787): Error inserting fileName=execute eTime=13  
 imgId=125 sTime=12 eventName=Execute 1.2 day=1 subStatus=0
01-30 01:31:53.141: E/Database(8787): android.database.sqlite.SQLiteException: table  
EventData has no column named subStatus: , while compiling: INSERT INTO  
EventData(fileName, eTime, imgId, sTime, eventName, day, subStatus) VALUES(?, ?, ?, ?,     
?,?, ?);

1 个答案:

答案 0 :(得分:0)

更改数据库时,只需删除并重新安装该应用程序即可。这将解决问题