I created a database, first I add object gives me a size = 1. But when i add a second object and i check the querycount(of all objects) i see that my database is filled with my first object (Count = 663) + my second object. If i Add a third item its infinity of the first object + second + third object.
DBHelper
public class MySQLiteHelper extends SQLiteOpenHelper {
public static final String TABLE_FILMS = "films";
public static final String COLUMN_ID = "id";
public static final String COLUMN_FILM = "filmnaam";
public static final String COLUMN_GENRE = "genre";
public static final String COLUMN_AVERAGERATING = "avgRating";
public static final String COLUMN_MYRATING = "myRating";
public static final String COLUMN_FAVORIET = "favoriet";
private static final String DATABASE_NAME = "favorieteFilms.db";
private static final int DATABASE_VERSION = 1;
// Database creation sql statement
private static final String DATABASE_CREATE = "create table "
+ TABLE_FILMS + "(" + COLUMN_ID + " text primary key, "
+ COLUMN_FILM + " text not null, "
+ COLUMN_GENRE + " text not null, "
+ COLUMN_AVERAGERATING + " text not null, "
+ COLUMN_MYRATING + " text null, "
+ COLUMN_FAVORIET + " integer null);";
public MySQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(MySQLiteHelper.class.getName(),
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_FILMS);
onCreate(db);
}
}
methods:
public class MySQLiteHelperDAO {
// Database fields
private SQLiteDatabase database;
private MySQLiteHelper dbHelper;
private String[] allColumns = { MySQLiteHelper.COLUMN_ID,
MySQLiteHelper.COLUMN_FILM, MySQLiteHelper.COLUMN_GENRE, MySQLiteHelper.COLUMN_AVERAGERATING,
MySQLiteHelper.COLUMN_MYRATING, MySQLiteHelper.COLUMN_FAVORIET};
public MySQLiteHelperDAO(Context context) {
dbHelper = new MySQLiteHelper(context);
}
public void open() throws SQLException {
database = dbHelper.getWritableDatabase();
}
public void close() {
dbHelper.close();
}
public Movie createMovie(Movie movie) {
ContentValues values = new ContentValues();
values.put(MySQLiteHelper.COLUMN_ID, movie.getId());
values.put(MySQLiteHelper.COLUMN_FILM, movie.getFilmnaam());
values.put(MySQLiteHelper.COLUMN_GENRE, movie.getGenre());
values.put(MySQLiteHelper.COLUMN_AVERAGERATING, movie.getRating());
values.put(MySQLiteHelper.COLUMN_MYRATING, movie.getMyRating());
values.put(MySQLiteHelper.COLUMN_FAVORIET, 1);
database.insert(MySQLiteHelper.TABLE_FILMS, null,values);
Cursor cursor = database.query(MySQLiteHelper.TABLE_FILMS, allColumns, MySQLiteHelper.COLUMN_ID + " = " + movie.getId(), null,null, null, null);
cursor.moveToFirst();
Movie newMovie = cursorToMovie(cursor);
cursor.close();
return newMovie;
}
public void deleteMovie(Movie movie) {
String id = movie.getId();
System.out.println("Movie deleted with id: " + id);
database.delete(MySQLiteHelper.TABLE_FILMS, MySQLiteHelper.COLUMN_ID
+ " = " + id, null);
}
public List<Movie> getAllMovies() {
List<Movie> movies = new ArrayList<Movie>();
Cursor cursor = database.query(MySQLiteHelper.TABLE_FILMS,
allColumns, null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Movie movie = cursorToMovie(cursor);
movies.add(movie);
cursor.moveToNext();
}
// make sure to close the cursor
cursor.close();
return movies;
}
private Movie cursorToMovie(Cursor cursor) {
Movie movie = new Movie();
cursor.moveToFirst();
movie.setId(cursor.getString(0));
movie.setFilmnaam(cursor.getString(1));
movie.setGenre(cursor.getString(2));
movie.setRating(cursor.getString(3));
movie.setMyRating(cursor.getString(4));
movie.setFavoriet(cursor.getInt(5));
return movie;
}
public Movie getMovie(String id) {
Movie movie = new Movie();
Cursor cursor = database.query(MySQLiteHelper.TABLE_FILMS, allColumns, MySQLiteHelper.COLUMN_ID + " = " + id, null, null, null, null);
if(cursor.getCount()>0)
movie = cursorToMovie(cursor);
return movie;
}
}
Aucun commentaire:
Enregistrer un commentaire