2013-07-26 78 views
-1

當我嘗試喚醒應用程序時,出現以下消息。sqlite返回:錯誤代碼= 1,msg =沒有這樣的列:TELEPHONE,

sqlite returned: error code = 1, msg = no such column: TELEPHONE,** 

這裏是我的代碼:

public class RestaurantBDD { 

    public static final int VERSION = 1; 
    public static final String NOM_BDD = "restaurant.db"; 

    public static final String TABLE_RESTAURANTS = "restaurants"; 

    public static final String COL_ID = "ID"; 
    public static final int NUM_COL_ID = 0; 
    public static final String COL_NAME = "NAME"; 
    public static final int NUM_COL_NAME = 1; 
    public static final String COL_ADRESSE = "ADRESSE"; 
    public static final int NUM_COL_ADRESSE = 2; 
    public static final String COL_GENRE = "GENRE"; 
    public static final int NUM_COL_GENRE = 3; 
    public static final String COL_NOTES = "NOTES"; 
    public static final int NUM_COL_NOTES = 4; 
    public static final String COL_TELEPHONE = "TELEPHONE"; 
    public static final int NUM_COL_TELEPHONE = 5; 

    private SQLiteDatabase bdd; 
    private RestaurantHelper restaurants; 

    public RestaurantBDD(Context context){ 
     restaurants = new RestaurantHelper(context, NOM_BDD, null, VERSION); 
    } 

    public void openForWrite(){ 
     bdd = restaurants.getWritableDatabase(); 
    } 

    public void openForRead(){ 
     bdd = restaurants.getReadableDatabase(); 
    } 

    public void close(){ 
     bdd.close(); 
    } 

    public SQLiteDatabase getBdd(){ 
     return bdd; 
    } 

    public long insertRestaurant(Restaurant restaurant){ 
     ContentValues content = new ContentValues(); 
     content.put(COL_NAME, restaurant.getNom()); 
     content.put(COL_ADRESSE, restaurant.getAdresse()); 
     content.put(COL_GENRE, restaurant.getGenre()); 
     content.put(COL_NOTES, restaurant.getNotes()); 
     content.put(COL_TELEPHONE, restaurant.getTelephone()); 
     return bdd.insert(TABLE_RESTAURANTS, null, content); 
    } 

    public int updateRestaurant(int id, Restaurant restaurant){ 
     ContentValues content = new ContentValues(); 
     content.put(COL_NAME, restaurant.getNom()); 
     content.put(COL_ADRESSE, restaurant.getAdresse()); 
     content.put(COL_GENRE, restaurant.getGenre()); 
     content.put(COL_NOTES, restaurant.getNotes()); 
     content.put(COL_TELEPHONE, restaurant.getTelephone()); 
     return bdd.update(TABLE_RESTAURANTS, content, COL_ID + " = " + id, null); 
    } 

    public int removeRestaurant(String nom){ 
     return bdd.delete(TABLE_RESTAURANTS, COL_NAME + " = " + nom, null); 
    } 

    public Restaurant getRestaurant(String nom){ 
     Cursor c = bdd.query(TABLE_RESTAURANTS, new String[] { COL_ID, COL_NAME, COL_ADRESSE,COL_GENRE, COL_NOTES, COL_TELEPHONE}, 
       COL_NAME + " LIKE \"" + nom + "\"", null, null, null, COL_NAME); 
     return cursorToRestaurant(c); 
    } 

    public Restaurant cursorToRestaurant(Cursor c){ 
     if(c.getCount() == 0){ 
      c.close(); 
      return null; 
     } 
     Restaurant restaurant = new Restaurant(); 
     restaurant.setId(c.getInt(NUM_COL_ID)); 
     restaurant.setNom(c.getString(NUM_COL_NAME)); 
     restaurant.setAdresse(c.getString(NUM_COL_ADRESSE)); 
     restaurant.setGenre(c.getString(NUM_COL_GENRE)); 
     restaurant.setNotes(c.getString(NUM_COL_NOTES)); 
     restaurant.setTelephone(c.getString(NUM_COL_TELEPHONE)); 
     c.close(); 
     return restaurant; 
    } 

    public ArrayList<Restaurant> getAllRestaurants(){ 
     Cursor c = bdd.query(TABLE_RESTAURANTS, new String[] { COL_ID, COL_NAME, COL_ADRESSE, COL_GENRE, COL_NOTES, COL_TELEPHONE}, 
       null, null, null, null, COL_NAME); 
     if(c.getCount() == 0){ 
      c.close(); 
      return null; 
     } 
      ArrayList<Restaurant> restaurantList = new ArrayList<Restaurant>(); 
      while(c.moveToNext()){ 
       Restaurant restaurant = new Restaurant(); 
       restaurant.setId(c.getInt(NUM_COL_ID)); 
       restaurant.setNom(c.getString(NUM_COL_NAME)); 
       restaurant.setAdresse(c.getString(NUM_COL_ADRESSE)); 
       restaurant.setGenre(c.getString(NUM_COL_GENRE)); 
       restaurant.setNotes(c.getString(NUM_COL_NOTES)); 
       restaurant.setTelephone(c.getString(NUM_COL_TELEPHONE)); 
       restaurantList.add(restaurant); 
      } 
      c.close(); 
      return restaurantList; 
    } 
} 

我的其他類

public class RestaurantHelper extends SQLiteOpenHelper{ 

    public static final String TABLE_RESTAURANTS = "restaurants"; 
    public static final String COL_ID = "ID"; 
    public static final String COL_NAME = "NAME"; 
    public static final String COL_ADRESSE = "ADRESSE"; 
    public static final String COL_GENRE = "GENRE"; 
    public static final String COL_NOTES = "NOTES"; 
    public static final String COL_TELEPHONE = "TELEPHONE"; 


    private static final String CREATE_BDD = "CREATE TABLE " + TABLE_RESTAURANTS + 
      " (" + 
      COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + 
      COL_NAME + " TEXT NOT NULL, " + 
      COL_ADRESSE + " TEXT NOT NULL, " + 
      COL_GENRE + " TEXT NOT NULL, " + 
      COL_NOTES + " TEXT NOT NULL, " + 
      COL_TELEPHONE + " TEXT NOT NULL " + "); "; 




    public RestaurantHelper(Context context, String name,CursorFactory factory, int version) { 
     super(context, name, factory, version); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     db.execSQL(CREATE_BDD); 

    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     db.execSQL("DROP TABLE"+ TABLE_RESTAURANTS); 
     onCreate(db); 
    } 

} 

我的logcat:

: sqlite returned: error code = 1, msg = table restaurants has no column named TELEPHONE, db=/data/data/com.example.restaurant/databases/restaurant.db 
: Error inserting GENRE=take-out ADRESSE=brossard NAME=mcdo TELEPHONE=450-555-5555 NOTES=fastfood 
: android.database.sqlite.SQLiteException: table restaurants has no column named TELEPHONE: , while compiling: INSERT INTO restaurants(GENRE,ADRESSE,NAME,TELEPHONE,NOTES) VALUES (?,?,?,?,?) 
at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method) 
at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:68) 

我收到以下消息時,我嘗試勞克的應用。 sqlite的返回:錯誤碼= 1,味精=沒有這樣的列:電話,**

+1

請顯示詳細的LogCat。它有助於找出錯誤的原因。 –

+0

只是爲了測試。您是否嘗試刪除de數據庫並重新加載應用程序以創建它?然後檢查TELEPHONE字段是否存在用SQLite DB瀏覽器打開。 –

+0

是的,卸載你的應用程序並重新安裝它,你可能首先使用以前的代碼創建你的數據庫,而不使用TELEPHONE列,那麼如果你重新部署它,它不會重新創建你的數據庫 – Blundell

回答

2

你有2種選擇:

  1. 刪除您的SQLite數據庫。這將調用OnCreate方法,並將使用新字段TELEPHONE創建數據庫。
  2. 更改數據庫版本。
public DatabaseOpenHelper(Context aContext) { 
     super(aContext, DATABASE_NAME, null, 2); 
} 

這將使調用OnUpgrade()方法刪除您的數據庫,並再次創造。

相關問題