运行应用程序时SQLite日志错误

时间:2017-06-19 08:20:18

标签: android android-sqlite

创建数据库时,我遇到以下错误:

  

E / SQLiteLog:(1)table table_place没有名为Fro的列   E / SQLiteDatabase:插入Toplace = Banglore Fro = Delhi时出错                                                                                  table table_place没有名为Fro(code 1)的列:,编译时:   INSERT INTO table_place(Toplace,Fro)VALUES(?,?)

这是db handler的代码:

 private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "Place.db";
    private static final String TABLE_PLACE = "table_place";
    private static final String KEY_ID = "id";
    private static final String KEY_FROM="Fro ";
    private static final String KEY_TO ="Toplace ";

    //Calling a constructor

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

    //Overriding methods


    @Override
    public void onCreate(SQLiteDatabase db) {

        String CREATE_PLACE_TABLE = "CREATE TABLE " + TABLE_PLACE + " ("
                + KEY_ID + " INTEGER PRIMARY KEY, " + KEY_FROM + " TEXT, "
                + KEY_TO + " TEXT);";
        db.execSQL(CREATE_PLACE_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        db.execSQL("DROP TABLE IF EXISTS " + TABLE_PLACE);
        onCreate(db);

    }

    //Adding data
    public void save(Place place){
        SQLiteDatabase db=this.getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put(KEY_FROM, place.getFrom());
        values.put(KEY_TO, place.getTo());

        db.insert(TABLE_PLACE, null, values);
        db.close();
    }

    //Finding data
    public Place findOne(int id){
        SQLiteDatabase db=this.getReadableDatabase();
        Cursor cursor=db.query(TABLE_PLACE, new String[]{KEY_ID,KEY_FROM,KEY_TO},
                KEY_ID+" =? ", new String[]{String.valueOf(id)}, null, null, null);
        if(cursor!=null){
            cursor.moveToFirst();
        }
        return new Place(Integer.parseInt(cursor.getString(0)),cursor.getString(1),cursor.getString(2));
    }

    //Finding all data
    public List<Place> findAll(){
        List<Place> listplace=new ArrayList<Place>();
        String query="SELECT * FROM "+ TABLE_PLACE;

        SQLiteDatabase db=this.getReadableDatabase();
        Cursor cursor=db.rawQuery(query, null);

        if(cursor.moveToFirst()){
            do{
                Place place=new Place();
                place.setId(Integer.valueOf(cursor.getString(0)));
                place.setFrom(cursor.getString(1));
                place.setTo(cursor.getString(2));
                listplace.add(place);
            }while(cursor.moveToNext());
        }

        return listplace;
    }

    //to update
    public void update(Place place){
        SQLiteDatabase db=this.getWritableDatabase();

        ContentValues values=new ContentValues();
        values.put(KEY_FROM , place.getFrom());
        values.put(KEY_TO, place.getTo());

        db.update(TABLE_PLACE, values, KEY_ID+" =? ", new String[]{String.valueOf(place.getId())});
        db.close();
    }

    //to delete
    public void delete(Place place){
        SQLiteDatabase db=this.getWritableDatabase();
        db.delete(TABLE_PLACE, KEY_ID+" =? ", new String[]{String.valueOf(place.getId())});
        db.close();
    }
}

主要活动:

    Log.d("insert", "inserting data");
    databaseHandler.save(new Place("Delhi", "Banglore"));
    databaseHandler.save(new Place("Chennai", "Mumbai"));

    Log.d("reading", "reading all data");
    List<Place> listplace = databaseHandler.findAll();
    for (Place b : listplace) {
        Log.d("data", "ID :" + b.getId() + " | Fro :" + b.getFrom() + " | Toplace :" + b.getTo());

1 个答案:

答案 0 :(得分:1)

删除空格。

private static final String KEY_FROM="Fro";

KEY_TO做同样的事情。更改代码后,请确保再次重新安装应用程序。