sqlite返回:错误代码= 1,msg =没有这样的列:TELEPHONE,

时间:2013-07-26 22:58:16

标签: android sqlite

当我尝试使用该应用程序时,我收到以下消息。

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,msg =没有这样的列:TELEPHONE,**

1 个答案:

答案 0 :(得分:2)

您有两个选择:

  1. 删除SQLite数据库。这将调用OnCreate方法,您将使用新字段TELEPHONE创建数据库。
  2. 更改数据库版本。
  3. public DatabaseOpenHelper(Context aContext) {
            super(aContext, DATABASE_NAME, null, 2);
    }
    

    这将调用OnUpgrade()方法删除你的Db并再次创建。