在Android中向SQLite添加第二个表时崩溃

时间:2014-10-25 15:03:50

标签: android sqlite android-sqlite sqliteopenhelper

我创建了一个带有一个表的SQLite数据库,它运行良好。然后我添加了第二个会崩溃的表。增加了db的版本,改名,无济于事。甚至颠倒了调用表的顺序:首先调用#2时工作,调用#1时崩溃。

当它在表#1的SQLiteHelper中调用游标时,应用程序似乎挂起。 logcat是:

  

android.database.sqlite.SQLiteException:没有这样的表:films(代码1):,同时编译:SELECT * FROM films

以下是表#1的帮助:

public class FilmSQLiteHelper extends SQLiteOpenHelper {
public FilmSQLiteHelper(Context context) {
    super(context, DatabaseContract.DB_NAME, null, DatabaseContract.DB_VERSION);
}

public void onCreate(SQLiteDatabase db) {
    db.execSQL(DatabaseContract.TableFilm.CREATE_TABLE);
}

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    Log.w(FilmSQLiteHelper.class.getName(),
            "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
    db.execSQL(DatabaseContract.TableFilm.DELETE_TABLE);
    onCreate(db);
}

// CRUD operations (create "add", read "get", update, delete)
public void addFilm(Film film) {
    Log.d(DatabaseContract.TableFilm.TAG, "add film " + film.toString());
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(DatabaseContract.TableFilm.COLUMN_1, film.getFilmName());
    values.put(DatabaseContract.TableFilm.COLUMN_2, film.getFilmType());
    values.put(DatabaseContract.TableFilm.COLUMN_3, String.valueOf(film.getFilmEi()));
    values.put(DatabaseContract.TableFilm.COLUMN_4, String.valueOf(film.getFilmRc1()));
    values.put(DatabaseContract.TableFilm.COLUMN_5, String.valueOf(film.getFilmRc2()));
    values.put(DatabaseContract.TableFilm.COLUMN_6, String.valueOf(film.getFilmRc3()));
    values.put(DatabaseContract.TableFilm.COLUMN_7, String.valueOf(film.getFilmRc4()));
    values.put(DatabaseContract.TableFilm.COLUMN_8, String.valueOf(film.getFilmRc5()));
    values.put(DatabaseContract.TableFilm.COLUMN_9, String.valueOf(film.getFilmRc6()));
    values.put(DatabaseContract.TableFilm.COLUMN_10, String.valueOf(film.getFilmRc7()));
    db.insert(DatabaseContract.TableFilm.TABLE_NAME_FILM, null, values);
    db.close();
}

public Film getFilm(long id) {
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursorFilm = db.query(DatabaseContract.TableFilm.TABLE_NAME_FILM,
            DatabaseContract.TableFilm.COLUMNS,
            " id = ?", // c. selections
            new String[]{String.valueOf(id)}, // d. selections args
            null, // e. group by
            null, // f. having
            null, // g. order by
            null); // h. limit
    if (cursorFilm != null) cursorFilm.moveToFirst();
    Film film = new Film();
    film.setId(Long.parseLong(cursorFilm.getString(0)));
    film.setFilmName(cursorFilm.getString(1));
    film.setFilmType(cursorFilm.getString(2));
    film.setFilmEi(Integer.parseInt(cursorFilm.getString(3)));
    film.setFilmRc1(Float.parseFloat(cursorFilm.getString(4)));
    film.setFilmRc2(Float.parseFloat(cursorFilm.getString(5)));
    film.setFilmRc3(Float.parseFloat(cursorFilm.getString(6)));
    film.setFilmRc4(Float.parseFloat(cursorFilm.getString(7)));
    film.setFilmRc5(Float.parseFloat(cursorFilm.getString(8)));
    film.setFilmRc6(Float.parseFloat(cursorFilm.getString(9)));
    film.setFilmRc7(Float.parseFloat(cursorFilm.getString(10)));
    Log.d(DatabaseContract.TableFilm.TAG, "getFilm("+id+") " + film.toString());
    return(film);
}

public ArrayList<Film> getAllFilms(){
    ArrayList<Film> films = new ArrayList<Film>();
    String query = "SELECT * FROM " + DatabaseContract.TableFilm.TABLE_NAME_FILM;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursorFilm = db.rawQuery(query, null);
    Film film =  null;
    if (cursorFilm.moveToFirst()){
        do{
            film = new Film();
            film.setId(Integer.parseInt(cursorFilm.getString(0)));
            Log.d(DatabaseContract.TableFilm.TAG, "id: "+ cursorFilm.getString(0));
            film.setFilmName(cursorFilm.getString(1));
            film.setFilmType(cursorFilm.getString(2));
            film.setFilmEi(Integer.parseInt(cursorFilm.getString(3)));
            film.setFilmRc1(Float.parseFloat(cursorFilm.getString(4)));
            film.setFilmRc2(Float.parseFloat(cursorFilm.getString(5)));
            film.setFilmRc3(Float.parseFloat(cursorFilm.getString(6)));
            film.setFilmRc4(Float.parseFloat(cursorFilm.getString(7)));
            film.setFilmRc5(Float.parseFloat(cursorFilm.getString(8)));
            film.setFilmRc6(Float.parseFloat(cursorFilm.getString(9)));
            film.setFilmRc7(Float.parseFloat(cursorFilm.getString(10)));
            films.add(film);
        }
        while (cursorFilm.moveToNext());
    }
    Log.d(DatabaseContract.TableFilm.TAG, "get all films() " + films.toString());
    return (films);
}

public int updateFilm(Film film){
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put("filmname", film.getFilmName());
    values.put("filmtype", film.getFilmType());
    values.put("filmei",   film.getFilmEi());
    values.put("filmrc1",   film.getFilmRc1());
    values.put("filmrc2",   film.getFilmRc2());
    values.put("filmrc3",   film.getFilmRc3());
    values.put("filmrc4",   film.getFilmRc4());
    values.put("filmrc5",   film.getFilmRc5());
    values.put("filmrc6",   film.getFilmRc6());
    values.put("filmrc7",   film.getFilmRc7());
    int i = db.update(DatabaseContract.TableFilm.TABLE_NAME_FILM,
            values,
            DatabaseContract.TableFilm.COLUMN_ID+"=?",
            new String [] {String.valueOf(film.getId())});
    db.close();
    Log.d(DatabaseContract.TableFilm.TAG, "updated film " + film.toString());
    return(i);
}

public void deleteFilm(Film film){
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(DatabaseContract.TableFilm.TABLE_NAME_FILM,
            DatabaseContract.TableFilm.COLUMN_ID+" = ?",
            new String[] {String.valueOf(film.getId())});
    Log.d(DatabaseContract.TableFilm.TAG, "delete film " + film.toString());
    db.close();
}
}

1 个答案:

答案 0 :(得分:0)

This question有答案。

    try{
    String sql = "SELECT id FROM TABLE_NAME;";
    Cursor cursor = db.rawQuery(sql, null);
    cursor.close();
     }
    catch(SQLiteException s){  
    onCreate(db);
    //db.execSQL("create TABLE_NAME store (id integer primary key, name text)"); }