-1

当我尝试启动应用程序时收到以下消息。

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);
    }

}

我的日志猫:

: 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,**

4

1 回答 1

2

您有 2 个选项:

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

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

于 2013-07-27T00:04:00.243 回答